loop executes even when condition is not met

The loop below is frustrating me by executing while the conditions are not met.

Is this a bug? It keeps printing even though I clearly set value to zero.

function Update()
 
{
 
if(value !=0)
 
{
 
if (rigidbody.IsSleeping())
 
{
 
                    print("Sleeping");
 
                    print ("last value is" + value);
 
}
 
}
 
value = 0;
 
 
}

What does your “value” field start on? It also may depend on if anything else is changing your value field too.

If it starts on something non-zero and isn’t changed by anything else (e.g. nothing else does value=2; for example) then the above should run exactly once :slight_smile:

it starts out > 0.

And as shown in update I set it to zero and it still runs what is in the if loop.[quote=“KulestarUK, post:2, topic: 539751, username:KulestarUK”]
What does your “value” field start on? It also may depend on if anything else is changing your value field too.

If it starts on something non-zero and isn’t changed by anything else (e.g. nothing else does value=2; for example) then the above should run exactly once :slight_smile:
[/quote]

you could try it in a scene and see.

It runs just once for me :slight_smile: What type is your “value” variable? Also you could pop up your script in the inspector to see what value is whilst it plays (as you’re possibly setting it from somewhere else).

Alternatively you might be destroying and recreating the object that your script is attached to - that would cause it to act like its being reset.

Lol really?

I have a print in the if loop that constantly say value is 0.

Can you share your whole script/ any more of it? :slight_smile:

Cant share that :smile:… Kinda private property.

But you are right. I tried in another scene and it worked.

So there is something I need to fix.