NullReferenceException when accessing monobehaviour class of an instance of a prefab

Hey guys!

My apologies if this question comes up a lot, couldn’t find an answer on my own.

I have a simple problem: I have a game where I create an “food” object, but in a randomized location.

First I’ve managed put this functionality into my GameManager class, which oversees most of the major roles and modules of my program. This version worked without any issues.

Now I am trying to refactor my code, and put the randomization into the food’s own monobehaviour class (which I dubbed “FoodManager”), so you only have to instantiate the prefab, and it itself will take care of the rest. This is where I get into a problem.

Original, working code:

public class GameController : MonoBehaviour
{
    public GameObject FoodPrefab;
    private void SpawnAFood()
    {
        Vector3 FoodLocation = new Vector3((Random.Range(-10, 10)),0,(Random.Range(-10, 10)));
        Quaternion FoodRotation = new Quaternion(0,0,0,0);
        GameObject FoodInstance = Instantiate(FoodPrefab, FoodLocation, FoodRotation);
    }
}

New, not working code:

public class GameController : MonoBehaviour
{
    public GameObject FoodPrefab;
    private void SpawnAFood()
    {
        GameObject FoodInstance = Instantiate(FoodPrefab);
        FoodInstance.GetComponent<FoodManager>().Relocate(); //second attempt to call this function
    }
}

And this is attached to the FoodPrefab:

public class FoodManager : MonoBehaviour {
    // Use this for initialization
    void Start () {
        //Relocate(); //original attempt to call this function, didn't work
    }
    public void Relocate ()
    {
        GetComponent<Rigidbody>().position = new Vector3((Random.Range(-10, 10)), 0, (Random.Range(-10, 10)));
    }
    // Update is called once per frame
    void Update () {
  
    }
}

In the not working code I’ve tried to first put the Relocate() into the Start() of FoodManager, so that the GameManager won’t have to call that one. That did not work, so I’ve started to experiment.

Thanks for the replies and help in advance!

You should add the complete error message including the stack trace.

And there’s no information where SpawnAFood is called from.

Anyway, there are not a lot of errors that could occur here, and Unity takes care of these in a different way by throwing other exceptions as these are components, i.e. they’ll be treated a little different (at least in the editor) to simplify error tracking. There’s actually no line in that code that would lead to a NullReferenceException in the editor if I’m not mistaken.

Those that would occur here are UnassignedReferenceException and MissingComponetException, both with a helpful error mesage.

So if you got a NullReferenceException, both appear different from what you’ve encountered.

I assume the error’s root is somewhere else.

If you look at the error message and the detailed stack trace, you’ll quickly find the root of all evil in most cases. If not, you can still start using the debugger and step through the code, you’ll usually see where the code runs into exceptional behaviour.