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 JDCAce55 · Apr 17, 2014 at 03:11 AM · c#gameobjectnullreferenceexception

Why are my GameObjects null when I add them to a List?

I have a GameController object with a GameController script that passes two GameObjects to a CombatManager script on the GameController. For some reason, after the GameObjects are passed, the CombatManager sees them as null, but only when I try to add them to a List. I don't even know where to begin. I've tried accessing the "name" field of the GameObject, and I can access it just fine, meaning it's not null, but when I try to put it in a List, I get a NullReferenceException!

Here's my GameController (in C#):

 public class GameController : MonoBehaviour
 {
     public GameObject combatant1, combatant2;
 
     public CombatManager combatManager;
 
     private void Start()
     {
         combatManager = gameObject.GetComponent<CombatManager>();
         combatManager.SetCombatants(combatant1, combatant2);
     }
 }

Here's my CombatManager (also in C#):

 public class CombatManager : MonoBehaviour
 {
     private List<GameObject> combatants;
 
     private void Start()
     {
         combatants = new List<GameObject>();
     }
 
     public void SetCombatants(GameObject combatant1, GameObject combatant2)
     {
         combatants.Add(combatant1); // Here is where Unity says the error is
         combatants.Add(combatant2);
         SortCombatantsBySpeed();
     }
 }

The exact error is this:

 NullReferenceException: Object reference not set to an instance of an object
 CombatManager.SetCombatants (UnityEngine.GameObject combatant1, UnityEngine.GameObject combatant2) (at Assets/Scripts/CombatManager.cs:16)
 GameController.Start () (at Assets/Scripts/GameController.cs:13)
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

3 Replies

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

Answer by Bunny83 · Apr 17, 2014 at 03:25 AM

If it's really line 12 where the null ref exception occurs, it's your "combatants" list which is null,not the GameObject reference. You probably are calling SetCombatants before Start has been executed. A simple Debug.Log in Start and SetCombatants would show that. Create the list in Awake instead.

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 JDCAce55 · Apr 17, 2014 at 03:34 AM 0
Share

That was it! I doubt I would've thought of that, thanks!

avatar image
0

Answer by Ermarrero · Apr 17, 2014 at 04:01 AM

you dont need to instantiate the list, since this script is attached to a gameObject, unity already instantiates it for you, that's why you can see the list in the inspector if you make it public. So remove it from the start function and just start adding to the list.

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 JDCAce55 · Apr 17, 2014 at 03:16 PM 0
Share

Are you sure about that? $$anonymous$$oving the instantiation to Awake (and modifying the script execution order) made my code work fine, but commenting out the instantiation brought the NullReferenceException back. Changing it to public and removing the instantiation worked, so maybe only public variables are automatically instantiated.

avatar image
0

Answer by JDCAce55 · Apr 17, 2014 at 04:18 PM

In addition to Bunny83's answer, which works, Unity advises users to use the Awake() method to "set up references between scripts, and use Start to pass any information back and forth." Using this advice also solves my problem.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Check if a component Exists on a Gameobject. 3 Answers

NullReferenceException error in an array of objects 0 Answers

Multiple Cars not working 1 Answer

node null reference 1 Answer

Null reference when accessing GameObject in the Array(C#) 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