How can this possibly be null?

mySquare = startSquare;
if(mySquare == null) print(“WHAT?”);

	while(mySquare != myGoal)
	{
		if(mySquare == null) print("Square Null");
    }

mySquare is not null before the While (“WHAT?” never gets printed), however, somehow, mySquare becomes null right after the while (“Square Null” always gets printed).

Can anyone explain to me how is that even possible?

Is this really ALL the code? Because it looks like an idiot loop as presented. If not your problem is probably in the code you didn’t bother to show us.

Assuming that this code is really the code you are running, then the answer has got to be that the variable is being changed by another thread. Make sure the variable is a local, not a global, to make sure something else isn’t setting it.

Otherwise its probably a lot more likely that either the code you are showing us is not the same code, or that its not really the code that is generating your output – there is either another copy of the code out there in another script OR you have attached this script to more then one object OR you have attached on an object more the once.

When you see something “impossible” in programming it ALWAYS means one of your assumptions is wrong. So go through all your assumptions one by one and start proving them right. You can start by showing us what you assumed was unimportant in the above code.

There are couple of possibilities.

  1. Your value is changed by a different thread. From the snippet I suspect you are fairly new coder so it might be the code you copied/included from third party.

  2. Square Null prints so fast, “What?” string goes out of bounds (if I remember correctly console holds 500 outputs) and your conclusions are wrong.

I can’t think of anything else from what little you showed here.