- Home /
why declare boolean value before functions didnt work?
Hi Guys,
The clockIsPaused (boolean type) only returns true if I declare it in the function. But why at the top or before the function, it didn't work? I thought we can input all the values when we declare the var which type and before all function start?
(I know it's sounds silly,that function is where all the process of code start) I'm just curious.
#pragma strict
var clockIsPaused : boolean = true;
function Start ()
{
//clockIsPaused = true;// even if I put the boo lean in the declare top it,
//it is still as false, why?
}
function Update () {
Debug.Log(clockIsPaused);
}
Answer by Paulius-Liekis · Oct 29, 2012 at 02:13 PM
Because it is automatically public and it is set to false in on our GameObject. Once the script is attached to GameObject values are taken from GameObject (in the inspector), not from declaration.
Solution: make it private. Or change it on start/awake.
Thanks Paulius,
can I assume that when all public var is null or false when its script is attached to GameObject? (if we didn't key in any value in the Inspector)
Yes and no. It will take default value or value that you defined in the declaration. So if you have declaration "var myInt : int", then value will be 0 (because default for int is 0), but if you have "var myInt : int = 5", then 5 will be taken as initial value when you attach the script.
It's weird, I removed my script, And edited and attach my script again.
This time, my new var booleans and var Integer's values all shown correctly to what I declared on the top. ( I declared 2 booleans, one true one false & one int : 99)
But didn't you say default value is zero & I experienced that it didn't shown correctly even i declared on the top.
1) How come it's all returning correct value now?
2) back to what you were saying, if assu$$anonymous$$g I didn't encounter the problems I said now, for lets say float or int values, it will return correctly of what I declared on the top even it's public, right?
I realize, if i Removed it or reset it, the values will return correctly (the top declare vars)
3) So the Inspector values of var is always null(except int), until I reset or remove & attach the script again?
Please let me know if I get it right, thanks.