- Home /
Howto access global var from other script?
So i followed these instructions here
TheScriptName.someGlobal = 10; http://docs.unity3d.com/Documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html
However, it yields an error when i follow it.
Do i have to call the GameObject first?
The variabel i try to access is
public static int Score = 0;
// a script called PlayerStats assigned to "Player"
Now i want to do this from another script:
// Score!
Player.Score++;
Answer by whydoidoit · May 18, 2013 at 09:06 PM
If the script is called PlayerStats then you need to write:
PlayerStats.Score++;
Yes, i tried it but did not work :(
Update:
I tried it both.
1 script is C#Sharp a´the other is Javascript. That a the problem?
Yes, don't mix languages it causes all sorts of problems (you can make it work one way by putting the script to be accessed in Plugins, but you can't go both ways ever).
It's really not a good plan to mix anything that you write yourself.
Yes the most difficult part is to access C# variables from Javascript because this requires you to put the C# script in the standardassets, Plugin, ... Folder (more information here http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html ) like whydoidoit said.
Another similar question: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html
Answer by SuperRyn · Jan 31, 2019 at 03:37 PM
public Class SampleClass1 : MonoBehaviour public static int Example = 1;
void Update () { if(Input.GetMouseDown(0)) {Example = 0}
} //In the class SampleClass2, int Example2; void Update () {Example2 = SampleClass1.Example}(); {if(Example2 = 0) {Debug.log("This was an example of global vars in C#")} }