What does "Object reference not set to an instance of an object," mean?

I am working on a script with C# and whenever I run my game, console pops up and says "Object reference not set to an instance of an object. Then when I click it to try and solve the problem, it brings me to this point in my script and I can't figure out what to do...

//Add spheres that can be collided against GameObject[] sp2 = GameObject.FindGameObjectsWithTag ("Softbody"); Object[] objs = GameObject.FindObjectsOfType(typeof(GameObject)); allSpheres = new ArrayList(); int cnt=0; for(int i=0;i<sp2.Length;i++) { allSpheres.Add(sp2[i]); cnt++; } for(int i=0;i<objs.Length;i++) { if(objs[i].name == "SphereSoft") { allSpheres.Add(objs[i]); cnt++; }


Could someone help me out? I will be continuously trying to find my answer on the Unity website.

more ▼

asked Dec 19, 2009 at 12:49 AM

avatar image

Colin Vinson
13 1 3 9

Can you give the source for your entire script? Or at least the entire function if it's very long.

Dec 19, 2009 at 01:22 AM Stelimar

There you go that should be it. Unless you need more. I could do that. There is another part describing the object allSpheres. Do you need that?

Dec 19, 2009 at 01:46 AM Colin Vinson
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Is that findgameobject function returning anything? It sounds like you're assigning a null response to that array.

more ▼

answered Dec 19, 2009 at 01:49 AM

avatar image

311 2 2 6

No it's not null it's just that Unity can't find the object to create and I don't know how to show which object.

Dec 19, 2009 at 01:54 AM Colin Vinson

I hink it has to do with sp2 not being properly initialized. Try something like GameObject [] sp2 = new GameObject[];

and then try the assignment from the findgameobject function.

Google the error comes up win lots of examples and the common ones seemed to be arrays (or other variables) not properly initialized before assignment.

Dec 19, 2009 at 04:04 AM jester

Should I make a new line after GameObject[]; in "GameObject[] sp2 = new GameObject[];?

Dec 19, 2009 at 04:39 PM Colin Vinson

can you either include more code here or private message me more code? i can't reproduce that error. from google searches i'm near convinced it's an initialization error but i can't yet reproduce that error myself to continue helping you.

here's one of many links that do similar array creation and returns that error when trying to access a member of the array that is actually null: http://www.velocityreviews.com/forums/t75867-static-problem-object-reference-not-set-to-an-instance-of-an-object.html

Dec 19, 2009 at 07:37 PM jester
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 19, 2009 at 12:49 AM

Seen: 18908 times

Last Updated: Dec 19, 2009 at 01:45 AM