- Home /
Accessing a js static var from a c# script
I have a static variable inside a JavaScript script. How can I access it from inside a C# script?
Answer by Ray-Pendergraph · Apr 03, 2011 at 08:16 PM
UnityScript files get compiled to classes underneath just like C# files. You can access a static field the same way as C# class. For unity script the class name is the file name so it should be <script file name without the js>.<field name>
. Classes from unity script files never have a package. One thing to consider is the compile order because you can't breach the language boundary within a given compile phase. That's what SendMessage is for.
Edit: Here is an example of how this works. ACSharpScript.cs goes in some folder I define to ensure it's compiled last:
public class ACSharpScript : MonoBehaviour {
void Update () {
AUnityScript.COUNTER++;
}
}
...then, in Standard Assets I create a UnityScript file called AUnityScript.js :
public static var COUNTER:int = 0;
function Update () {
print(COUNTER);
}
the C# is incrementing the static COUNTER field in the UnityScript class and an instance of the UnityScript (the instance created when you hang it on the graph) is reading the static field every frame. Hang these on the scene and try it... you will see COUNTER incrementing every frame.
Thanks Ray but could you be more specific? I mean, I have a static var myStaticVar: int in a UnityScript (named $$anonymous$$yJSScript). Under certain conditions, I want a c# script to increase myStaticVar by one. Writing (in the C# script): $$anonymous$$yJSScript.myStaticVar +=1; doesn't work. What is the correct syntax?
Doesn't compile or doesn't work? That looks right to me. You can access C# fields from unity script and unity script fields from C#. The important thing is the compile order described in the link. If one language is to call another in another language the target class must be compiled before the caller.
At first, I had both scripts (UnityScript and C#) inside a folder named "Scripts" and I was getting the error: error CS0103: The name '$$anonymous$$yJSScript' does not exist in the current context. After that, I moved the UnityScript inside the Plugins folder (so that it's compiled first). This way I got rid of the error, but what I am telling the c# script to do ($$anonymous$$yJSScript.myStaticVar +=1) simply doesn't happen.
$$anonymous$$ay be something else going on... we do this with a few UnityScript components that we did not write. Our code base is C# too. If you are making it past the compilation you should be O$$anonymous$$ with this. $$anonymous$$aybe try a different test case or something like setting the the thing to 20 and reading it from C#.
@schwertfisch - I updated the post with a simple example.
Your answer
Follow this Question
Related Questions
One of my scripts can't find the variable from this script 1 Answer
Help with variables 2 Answers
Cannot use Static var/ Unknown Idientifier 1 Answer
Is it possible to create new variables by incrementing? 1 Answer
Static Variables 1 Answer