Script not finding other script

I’ve got a “scr_Game_State.js”-script that stores static variables about the state of the game. For instance:

scr_Game_State.js

static var gamestate_Player_Direction = "right";

This script is attached to an empty object that is placed into the scene.

Normally I access these static variables from other scripts by using this method:

// Init:	Script
private var Game_State : scr_Game_State;	
// Set:	Walk left
Game_State.gamestate_Player_Direction = "left";

This works for almost all scripts, except for wanting to check it inside the “Joystick.js” (that comes with unity’s sidescroller template for the iphone).

Instead I get the following error related to the initiation of the script (ie. “private var Game_State : scr_Game_State;”):

“Assets/iPhone Standard Assets/Scripts/Joystick.js(92,26): BCE0018: The name ‘scr_Game_State’ does not denote a valid type (‘not found’). Did you mean ‘System.IO.SeekOrigin’?”

What could be causing this? Is perhaps “Joystick.js” executed before “scr_Game_State.js” meaning at that time there is no such script to reference? If that is the case, how do I circumvent this? Do I need to place “scr_Game_State.js” on something else that gets initiated earlier in the setup-process of the scene, or can I set the empty object that contains the script to have a higher priority in the setup or something? Or am I way off here?

If it’s static, then you should be using it like:

scr_Game_State.gamestate_Player_Direction = “left”;

Normally, when I call a variable from a specific object I reference the script on the object.

var GameScript=myGameObject.gameObject.GetComponent("ScriptToRead");

var MyVariable=GameScript.VariableToRead;

//OR...

var MyVariable=myGameObject.gameObject.GetComponent("ScriptToRead").VariableToRead;

Both your suggested methods work fine when used in any other script than “Joystick.js”, which is what lead me to draw the half-cooked conclusion about the gameobject containing “scr_Game_Script.js” being created AFTER “Joystick.js” references it.

FizixMan’s code gives:
“Assets/iPhone Standard Assets/Scripts/Joystick.js(329,41): BCE0005: Unknown identifier: ‘scr_Game_State’.”

And BigMisterB’s code equally gives:
“Assets/iPhone Standard Assets/Scripts/Joystick.js(329,56): BCE0005: Unknown identifier: ‘myGameObject’.”

Joystick.js is in a Standard Assets - that folder is compiled before some other code. (Plugins folder is also compiled earlier, but I don’t know which of the two comes first). If you move Joystick out of that folder, you shoulod get rid of that problem. If Joystick relies on anything else in that folder you may have to move that/those as well.

Ok, so my speculation wasn’t completely off then.

I actually went ahead and put the “scr_Game_State.js” file in the standard assets-folder to avoid any assumed complications by moving the more complex “Joystick.js” out of it, and now it works just fine and dandy!

“scr_Game_State.js” gets compiled early enough now to be recognized by “Joystick.js”.

Thank you very much, Vicenti, for solving this problem that was starting to drive me bananas, and thanks to the rest of you for your suggestions :slight_smile:

Of course, if this is a repeating thing then you can simply do a try catch (or whatever it is in C#) :wink: that way if it calls it without it being created it simply catches and returns without causing error.

try{scr_Game_State.gamestate_Player_Direction = “left”; }catch(e){return;}

stupid double posting… :frowning:

Thats true - since this was in fact being called from the Update()-function, this code should have worked as well. Thanks for the tip - I’ll consider it for future reference since the current solution works fine :slight_smile: