• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Colin Vinson · Dec 19, 2009 at 12:49 AM · physicsnullreferenceexception

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stelimar · Dec 19, 2009 at 01:22 AM 0
Share

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

avatar image Colin Vinson · Dec 19, 2009 at 01:46 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jester · Dec 19, 2009 at 01:49 AM

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Colin Vinson · Dec 19, 2009 at 01:54 AM 0
Share

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.

avatar image jester · Dec 19, 2009 at 04:04 AM 0
Share

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.

avatar image Colin Vinson · Dec 19, 2009 at 04:39 PM 0
Share

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

avatar image jester · Dec 19, 2009 at 07:37 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers

2D 360 degress platformer example needed 0 Answers

NullReferenceException: Object reference not set to an instance of an object Error 0 Answers

Making a pendulum? 3 Answers

Physics Unity Classic vs Physics Unity Dots 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges