Accessing a static variable between two gameobjects - JS to C sharp

Hi, I'm completely new to C Sharp so this is probably an easy question to most of you!

I'm trying to access a static variable "levelCompletefinalScore" in the "MainMenuGUI" script attached to the "Main Camera" game object but the "MainMenuGUI" script is a javascript file and I'm trying to access it from a C sharp script.

Basically, how can I access a static variable contained within a Javascript script with a C sharp script? So far I've got this;

public class Social : MonoBehaviour
    void Start()
        GameObject cameraGO = GameObject.Find("Main Camera");
        MainMenuGUI mainMenuGUI = cameraGO.GetComponent<MainMenuGUI>();
        print (mainMenuGUI.levelCompletefinalScore);

Thanks for any help!

Static variables are accessed like this:



className.variableName; if you have classes

Well you don't have to declare the mainMenuGUI to access the static variable.

You just have to call the class and then the variable like so

MyClass: print(MyOtherClass.thisIsAStaticVariable);

MyOtherClass: public static string thisIsAStaticVariable = "This is a static variable";

I hope you understand otherwise this might explain it better


I found out why it was not working, you have to compile the javascript script before the C# script. Put your javascript file into a folder name "Standard Assets" and it should work.

Why? Check these links:

See the docs about script compilation order.