Mystery null reference exception

I have some code that works perfect 99.9% of the time, but 1/1000 tries I end up with a mystery null reference exception and I can’t figure out why.

The basics are that I have some gameObjects that I call “tiles”, and some other gameObjects that I call “stacks”. I drag the tiles onto the stacks to drop them onto the stacks, all handled through some basic colliders and standard Unity methods. I have a script on each “tile” object that includes this code:

void OnTriggerEnter2D(Collider2D other)
	{
		if (other.tag == "Stack" && !onStackOne)
		{
			stackBaseOne = other.gameObject;
			onStackOne = true;
		}

		else if (other.tag == "Stack" && stackBaseOne != other.gameObject)
		{
			stackBaseTwo = other.gameObject;
			onStackTwo = true;
		}
	}

void OnTriggerExit2D(Collider2D other)
	{
		if (other.gameObject == stackBaseOne)
			onStackOne = false;

		else if (other.gameObject == stackBaseTwo)
			onStackTwo = false;
	}

It’s done this way because it’s possible that I could be holding a “tile” over 2 separate “stacks” simultaneously (it’s not possible to have a tile over 3 simultaneous stacks). When I let go of the tile, this code handles dropping it onto the stack:

void OnMouseUp()
	{

		if (!onStackOne && !onStackTwo)
		{
			//code to snap tile back to previous position
	}
			
	else if (onStackOne && !onStackTwo)
	{
		stack = stackBaseOne.GetComponent<Stackable> ();
		//code to handle dropping the tile on stack one
	}
			
	else if (!onStackOne && onStackTwo)
	{
		stack = stackBaseTwo.GetComponent<Stackable> ();
			//code to handle dropping the tile on stack two
	}
			
	else if (onStackOne && onStackTwo)
	{
		stackOneVal = stackBaseOne.GetComponent<Stackable> ().value;
		stackTwoVal = stackBaseTwo.GetComponent<Stackable> ().value;
		//code to handle determining which stack is closer and dropping it
	}
}

What’s happening is that very rarely, when I drop the tile on a stack, OnMouseUp() fires but I end up with a null reference exception at the first line of one of those last 3 if statements because the gameObject it’s referencing (either stackBaseOne or stackBaseTwo) has been destroyed. The only way this could happen is if the OnTriggerExit2D method didn’t fire correctly, so either onStackOne or onStackTwo is still showing ‘true’ when it should be ‘false’, and the referenced gameObject (which the tile is no longer hovering over) has since been destroyed, thus the null reference exception.

I’ve been staring at this code for days and I just cannot figure out what’s going wrong. Any idea?

I can say I know what pieces of code may be causing the problem, but why it is happening I do not know. It has to deal with the logic in which you are doing, but nonetheless I can help you resolve them.

The Exception arises when you are trying to use “value” from GetComponent(). Based on your code that is the only spot this error would arise, because you are accessing something that doesn’t exist.

Her is a safe way.

stack1 = stackBaseOne.GetComponent<Stackable> ();
if(stack1 != null)
    stackOneVal = stack1.value;


stack2 = stackBaseTwo.GetComponent<Stackable> ();
if(stack2 != null)
    stackTwoVal = stack2.value;

Because GetComponent can return null if an object does not contain the component. It is best you make sure it isn’t null before you start calling it’s methods.