Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by TSI25 · Dec 08, 2013 at 06:23 PM · nullreferenceexceptionunity4.3

Cant fix a null reference exception?

So I'm getting a null reference exception and I can't figure out how to fix it. Heres the code and line its on.

 private var ship : GameObject;
 private var planets : Array;
 private var stuckDistance : float;
 private var planet : GameObject;
 
 function Start () {
 ship = GameObject.Find("Ship");
 planets = new Array();
 planets.length = 4;
 planets[0] = GameObject.Find("Planet_1");
 planets[1] = GameObject.Find("Planet_2");
 planets[2] = GameObject.Find("Planet_3");
 planets[3] = GameObject.Find("Planet_4");
 stuckDistance = 15.0f;
 }
 
 function Update () {
     for (var p = 0; p < 4; p++)
     {        
         if((Vector3.Distance (transform.position, planets[p].transform.position)) <= stuckDistance)
         {
             planet = planets[p]; 
         }
     }
     transform.position = planet.transform.position;//line with null exception
     refueling = true;
 }
 
 my working theory right now is maybe i have to declare the array differently? I'm looking at the scripting reference but I'm not seeing anything very helpful. anyone have any ideas?
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Dec 08, 2013 at 06:37 PM

If you had used '#pragma strict' at the top of the file, your code would generate an error which I believe points you in the right direction. First avoid the 'Array' class. If you need an array and you know it size (i.e. it does not have to grow), use a built-in array. If it needs to grow or change size, consider a .NET generic List. Here is a reference:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

As for your specific bug here, delete lines 8 and 9 and declare your array on line 2 as:

 private var planets : GameObject[] = new GameObject[4];

Note you if you still get a null reference exception, then it is likely that your GameObject.Find() is failing. Check for exact spelling and case.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

itween example work well on unity3d while NullReferenceException on android 0 Answers

Null Reference Excepting :Object reference not set to an instance of object while changing text of 3D text 2 Answers

NullReference on textfield 1 Answer

Can I Ignore Null Reference Exceptions? ( bug? ) 3 Answers

NullReferenceException? 2 Answers


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