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:
scriptName.variableName;
or
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 http://msdn.microsoft.com/en-us/library/98f28cdx.aspx
EDIT:
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:
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
http://answers.unity3d.com/questions/3392/cant-find-namespace-accessing-javascript-from-c-or-c-from-javascript
See the docs about script compilation order.