.GetComponent returns Null

Hello everyone,

I am just getting started with Unity but there is a problem I just can’t solve in my own. I am following a YouTube Tutorial and downloaded it sources, which work totally fine. But when I try to rebuild it by myself I always get the error stated in the subject of this thread (Object reference not set to an instance of an object).

Let me explain this:

This is the method which throws the error:

bool IsLegalPosition() {
	if (placeableBuilding.colliders.Count > 0) {
		return false;	
	}
	return true;
}

It says that the placeableBuilding is Null at this point. This is set here in this method which is called before IsLegalPosition():

public void SetItem(GameObject b) {
	hasPlaced = false;
	currentBuilding = ((GameObject)Instantiate(b)).transform;
	placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
}

Somehow everything is fine until it comes to the .GetComponent-Method. The method returns null thus placeableBuilding always stays null and throws an error when read the first time in the code.

But why is that the case? The GameObject which I made has the Component PLaceableBuilding, which is a script too… Can anyone here help me with this problem?

My guess would be that your object currentBuilding does not contain a PlaceableBuilding component. Make sure you have added the PlaceableBuilding MonoBehaviour to it.

First of all, you are not doing any null checking. You should test them before you use them.

bool IsLegalPosition() 
{
    if (placeableBuilding != null &&
        placeableBuilding.colliders != null &&
        placeableBuilding.colliders.Count > 0)
    {
        return false;    
    }
    return true;
}

This is not generally valid logic, as if any of the above are null, you can’t tell if it’s true or false because you can’t actually do the test. You could make the return type nullable and return null if you hit nulls and only return true/false if you get to the test.

public bool? IsLegalPosition() 
{
    if (placeableBuilding == null || placeableBuilding.colliders == null) return null;

    return (placeableBuilding.colliders.Count > 0) ? return false : return true;
}

Another bit of error checking you can do is when finding the objects.

public void SetItem(GameObject item) 
{
    hasPlaced = false;

    currentBuilding = ((GameObject)Instantiate(item)).transform;
    if (currentBuilding == null)
    {
        Debug.LogError("currentBuilding failed to instantiate");

        // Return from the method as currentBuilding is null.
        // The attempt to use it will throw a null reference exception.
        return;
    }

    placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
    if (placeableBuilding == null)
    {
        Debug.LogError("placeableBuilding component was not found");
    }
}

Neither of these things will help you find your game object but they will help to avoid pointless errors and give you information in the console window that will let you know something is wrong.

At this point you need to find out why the component is missing. Try stepping through the code. Can you actually instantiate the prefab? If so, check your prefab. Does that prefab actually contain the component you want.