- Home /
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)
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.
That was it! I doubt I would've thought of that, thanks!
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.
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.
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.
Your answer
Follow this Question
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