x


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?

more ▼

asked Apr 03 '11 at 08:11 PM

schwertfisch gravatar image

schwertfisch
370 53 57 68

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Apr 03 '11 at 08:16 PM

raypendergraph gravatar image

raypendergraph
1.6k 14 21 37

Thanks Ray but could you be more specific? I mean, I have a static var myStaticVar: int in a UnityScript (named MyJSScript). Under certain conditions, I want a c# script to increase myStaticVar by one. Writing (in the C# script): MyJSScript.myStaticVar +=1; doesn't work. What is the correct syntax?

Apr 03 '11 at 08:22 PM schwertfisch

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.

Apr 03 '11 at 08:33 PM raypendergraph

At first, I had both scripts (UnityScript and C#) inside a folder named "Scripts" and I was getting the error: error CS0103: The name 'MyJSScript' 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 (MyJSScript.myStaticVar +=1) simply doesn't happen.

Apr 03 '11 at 08:41 PM schwertfisch

May 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 OK with this. Maybe try a different test case or something like setting the the thing to 20 and reading it from C#.

Apr 03 '11 at 09:04 PM raypendergraph

@schwertfisch - I updated the post with a simple example.

Apr 04 '11 at 12:07 PM raypendergraph
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3443
x342
x276
x107
x34

asked: Apr 03 '11 at 08:11 PM

Seen: 2747 times

Last Updated: Apr 03 '11 at 08:11 PM