- Home /
Why is this boolean value changing?
Is it not possible in UnityScript to change the value of a boolean variable with the NOT operator?
This is what I would expect:
var x = true;
var y = true;
var x = !y;
//variable x is now false
Is this not something you can do in UnityScript? I believe it works in regular JavaScript...
Answer by YoungDeveloper · Jul 24, 2015 at 09:28 PM
You are redeclaring variable x, otherwise your example will work. The problem is that your variable is probably overridden from inspector. Im a c# programmer mostly, but i tried this example and is works perfectly.
private var x:boolean = true;
private var y:boolean = true;
private var a:boolean = !y;
private function Start():void{
Debug.Log(a); //prints false
}
I actually put the var on here by accident, I didn't have it in my script I was working off of.
I would guess you may be right about the inspector overriding the value. I've had a ton of problems with that! I hate how Unity serializes all of my public variables, as I have several scripts that all share information with each other.
Unity would be pretty broken if it didn't, since otherwise any script change would destroy all of your carefully-set-up public variables on all objects that used that script. But you can use the NonSerialized attribute if you want a variable to be public but not serialized.
Same with private, if you want to serialize variable, use [SerializeField] before private.
Answer by Eric5h5 · Jul 24, 2015 at 09:29 PM
Sure you can. It works as expected, though you need to remove "var" from the third line since you can't declare the same variable twice.