Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Harabeck · Oct 05, 2011 at 01:37 AM · nullreferenceexceptionexception

NullReferenceException, but the reference is set!

NullReferenceException: Object reference not set to an instance of an object Squad.AddUnit (.Unit u) (at Assets/Scripts/Squad.cs:20) UnitManager.Start () (at Assets/Scripts/UnitManager.cs:44)

I'm getting a NullReferenceException (at units.Add(u);, see below) but I can't understand why. But by the time I've called AddUnit I've already checked that unit exists in the if statement, and done a print command with it (which prints as expected).

Here's AddUnit in Squad:

  public List<Unit> units;
         public bool selected=false;
         
         // Use this for initialization
         void Start () {
             
             units=new List<Unit>();
             print("units initialized: "+units.Count);
         
         }
     
     public void AddUnit(Unit u)
         {
             units.Add(u); //This is the line that throws the error
         }

Here's the only place AddUnit is called from:

 public List<Squad> squads;
     public int numberOfSquads=4;
     
     
     // Use this for initialization
     void Start () {
         
         squads=new List<Squad>();
         
         for(int i=0; i<numberOfSquads;i++)
         {
             squads.Add(new Squad());//create the number of squads we need, their position in the List will be their "id"
         }
         
         foreach(GameObject go in allUnitsList)
         {
             Unit unit;
             int squadID;
             Squad squad;
             
             unit=go.GetComponent<Unit>();
             
             if(unit)//make sure we have a Unit component attached
             {
                 squadID=unit.squadID;
                 squad=squads[squadID];
                 
                 print("squad building: "+unit.gameObject.name+" goes to squad "+unit.squadID+" squad selected: "+squad.selected);
                 /*squad building: PlayerUnit1_1 goes to squad 1 squad selected: False
                 UnityEngine.MonoBehaviour:print(Object)
                 UnitManager:Start() (at Assets/Scripts/UnitManager.cs:42)*/
                 
                 squad.AddUnit(unit);
                 
             }
         }
     
     }

I thought maybe units wasn't being initialized in time, so included the print to check, and it fires long before AddUnit is called. So squad, unit, and units are initialized when the function is called, so what exactly is null??

Comment
Add comment · Show 1
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 Harabeck · Oct 05, 2011 at 01:34 AM 0
Share

I tried replacing squad.AddUnit(unit); with squad.units.Add(unit); (units is public, so just reference it and directly call Add()) and now the error is thrown there.

NullReferenceException: Object reference not set to an instance of an object Unit$$anonymous$$anager.Start () (at Assets/Scripts/Unit$$anonymous$$anager.cs:48)

2 Replies

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

Answer by Harabeck · Oct 05, 2011 at 09:54 PM

Ok, figured it out. I wasn't using Squad as a monobehavior, just a C# class, so I needed to use the normal constructor.

 public class Squad {
     
     public List<Unit> units;
     public bool selected=false;
     
     public Squad()
     {
         units=new List<Unit>();
         Debug.Log("units should be init");
     }
Comment
Add comment · Show 1 · 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 vxssmatty · Oct 05, 2011 at 10:09 PM 0
Share

dont forget to mark yourself as the correct answer ;)

avatar image
0

Answer by Zogg · Oct 05, 2011 at 02:09 AM

Probably the second Start() method is called before the first one.

Try doing the initialization in Awake() instead of Start():

 void Awake() {
 
     units=new List<Unit>();
     print("units initialized: "+units.Count);
 
  }
Comment
Add comment · Show 1 · 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 Harabeck · Oct 05, 2011 at 09:05 PM 0
Share

Nope, no change, but it is definitely the List that is the problem.

Squad:

void Awake() { units=new List(); }

Unit$$anonymous$$anager:

void Awake() { squads=new List();

     for(int i=0; i<numberOfSquads;i++)
     {
         squads.Add(new Squad());//create the number of squads we need, their position in the List will be their "id"
     }
 }
 
 // Use this for initialization
 void Start () {

     foreach(GameObject go in allUnitsList)
     {
         Unit unit;
         int squadID;
         Squad squad;
         
         unit=go.GetComponent<Unit>();
         
         if(unit)//make sure we have a Unit component attached, maybe should switch allUnitsList to a List<Unit>?
         {
             squadID=unit.squadID;
             squad=squads[squadID];
             
             print("squad building: "+unit.gameObject.name+" goes to squad "+unit.squadID+" squad selected: "+squad.selected);
             print("units in squad: "+squad.units.Count);//error is here now
             /*squad building: PlayerUnit1_1 goes to squad 1 squad selected: False
             UnityEngine.$$anonymous$$onoBehaviour:print(Object)
             Unit$$anonymous$$anager:Start() (at Assets/Scripts/Unit$$anonymous$$anager.cs:42)*/
             
             //squad.AddUnit(unit);
             squad.units.Add(unit);
             
         }
     }
 
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

NullReferenceException help 1 Answer

Help with NullReferenceException 1 Answer

What can cause Unity to crash? 3 Answers

How can i find to Cool Main menu for iOS 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