Calling variables in external script - inconsistent results

Ive got a game-state script where I initialize and update variables that are "global".

Game_State.js:

var globalVariable_1 = "hello";  // string 
var globalVariable_2 = 20;  // number 
var globalVariable_3 = false;  // bool 

Other_Script.js:

private var Game_State : scr_Game_State; 
function Start () { 
   Game_State = FindObjectOfType(scr_Game_State);    

   Debug.Log("globalVariable_1 = " + Game_State.globalVariable_1); 
   Debug.Log("globalVariable_2 = " + Game_State.globalVariable_2); 
   Debug.Log("globalVariable_3 = " + Game_State.globalVariable_3); 
} 

Output:

globalVariable_1 = hello 
globalVariable_2 = 1 
globalVariable_3 = true 

As you can see, the output is correct only for the string-type, but when initializing numbers or booleans the value retured is incorrect.

Can anybody make sense of this, and hopefully propose a solution?

Well first of all you can declare your variables `static` and then you don't have to do the `FindObjectOfType` nonsense. Just call your variables exactly the same way you have them in your example code.

Second, try declaring the types of your variables. So, like this:

var globalVariable_2 : int = 20;

My guess is that you changed the values of your public variables at some point in the script, without realising (or forgetting - heck it happens all the time to me :P) how unity serializes public variables

What happens is that unity will keep on persisting the values which were set in the inspector (which will have been 1 for var2, true for var 3) after the first time they're set, and disregarding script changes for the values after that

If you want to change them, you should change them in the inspector for the script

Note - I still think you should use Tetrad's answer of making them static.

Thanks for your answers :)

Im definitely going with the static variables - works great!