- Home /
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?
I'm going to go on a hunch and say you're changing the value of mySquare in your Update() method or something of the like.
If it's a Coroutine that has been yielded, the Update() method could have very well changed the variable value by the time the yield ends (by all means of logic, I have no research to prove it).
But, there is no Update() in the script, furthermore, mySquare only exists within this function as a local variable, and is only changed within the While loop.
Answer by Jeff-Kesselman · May 30, 2014 at 11:07 PM
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.
The script is only attached to the player, I just checked and I only attached it once to the player, and the whole script is as follows:
function Awake(){
squareArray = GetComponent(Character).squareArray;
}
function $$anonymous$$akePath(startSquare : Square, endSquare : Square)
{
var mySquare : Square;
var myGoal : Square = endSquare;
mySquare = startSquare;
if(mySquare == null) print("WHAT?");
while(mySquare != myGoal)
{
if(mySquare == null) print("Square Null");
}
}
It is indeed the whole code (except a "var squareArray : List." before the Awake function); I know it is just an idiot loop doing nothing but I was just "testing the water" before adding any real code in.
Answer by Lovrenc · May 30, 2014 at 11:30 PM
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.
I agree with 2. @rkGio, try changing your while statement to
if(mySquare != myGoal){
if(mySquare == null) {
print("Square Null");
}
}
Thats a good idea. Also turn on collapse on your output. That will prevent repeated identical error messages from filling up your console.
If it worked my way, then it's working your way, but it's logging too fast for you to see it. There's no technical problem with the code, just the delivery of logging.
So it is point 2. Value of the mySquare variable never changes. It was null from the beginning. Everything works as intended.
Also, what @tw1st3d said in comment above is true and since you failed to realize this on your own I suggest you read about basic looping before you go on. (I am not saying that with bad intentions).
Your answer
![](https://koobas.hobune.stream/wayback/20220613150311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullReferenceException question. 2 Answers
3rd person controller NullReferenceException 2 Answers
Help with raycast == null 0 Answers