Null reference exception: Object reference not set to an instance of an object

I’m very new to Unity. Every other case of this problem I’ve seen has occurred because they have forgotten to instantiate a new list, but I think I’ve done that okay… I keep getting the aforementioned error at the line indicated with comment. Any ideas why? My goal is to spawn blocks and have them be automatically destroyed once they fall a certain distance. Thanks in advance.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnBox : MonoBehaviour {

    public Rigidbody SpawnedBox;
    static List<GameObject> SpawnedBoxList;

	void Start () 
    {
        SpawnedBoxList = new List<GameObject>();
	}

	void Update () 
    {

        if (Input.GetButtonDown("Jump")) 
        {
            GameObject instanceBox = Instantiate(SpawnedBox, transform.position, transform.rotation) as GameObject;
            SpawnedBoxList.Add((GameObject)instanceBox.gameObject); //NULL REFERENCE EXCEPTION HERE
        }
        

        //for (int i = 0; i < SpawnedBoxList.Count; ++i)
        //{
        //    if (SpawnedBoxList*.gameObject.transform.position.y < -100)*

// {
// Destroy(SpawnedBoxList*.gameObject);*
// }
//}
* }*
}

All null ref problems in the world could be solved EASILY with debugging.

PLEASE LEARN HOW TO DEBUG YOUR PROGRAM! (If you’re on MonoDev, or VS)

Either your box instance is null, and you’re adding a null to your list, or your list is null, you just gotta know what’s happening to narrow it down. If you want to manually debug it, here’s what you could do to nail it:

put a couple of logs at the right places:

if (Input.GetButtonDown("Jump"))
{
   GameObject instanceBox = Instantiate(SpawnedBox, transform.position, transform.rotation) as GameObject;
   if (instanceBox == null) {
      Debug.Log("instanceBox is null, problem instantiating");
      return;
   }

   if (SpawnedBoxList == null) {
      Debug.Log("list is null, initialize it properly, or something...");
      return;
   }
   SpawnedBoxList.Add((GameObject)instanceBox.gameObject);
}

You don’t need to cast your instanceBox to a GameObject because it already is, and you certainly don’t need to take the gameObject component from it (it would return itself) :slight_smile: (that might have been causing your problem?) - Just do:

SpawnedBoxList.Add(instanceBox);

Another thing, when you use a static variable, and have the chance to immediately give it a value the moment you initialize it, do it, like:

static List < GameObject > SpawnedBoxList = new List < GameObject > ();

If you’re just beginning, you might wanna be careful with statics, see this for proper use of them (skip to the ‘good coding habits’ part of the answer) - Do you really need to use a static? I’m sure you could get away without it :slight_smile:

Why have you declared the list static ?

Replace those two line with this,
And do not forget to assign the SpawnBox rigidbody in the inspector.

GameObject instanceBox = Instantiate(SpawnedBox.gameObject, transform.position, transform.rotation) as GameObject;
    SpawnedBoxList.Add(instanceBox);

NullReferenceException comes when you try to use an object that does not exist (that is, it is null). On the line you’ve quoted, there are two objects that could give your error, instanceBox, or SpawnedBoxList.

You’re doing a lot of unnecessary casting anyway. Since instanceBox is a GameObject, calling .gameObject on it just returns the same object, and casting it to a GameObject again does nothing, so you could reduce:

SpawnedBoxList.Add((GameObject)instanceBox.gameObject);

to just:

SpawnedBoxList.Add(instanceBox);

I think it more likely that SpawnedBoxList is null, though, so be sure to set it correctly (i.e. SpawnedBoxList = new List()).