Global Variables

I am trying to get an idea of how the variables are handled in Unity. I basically wanted to use an array to store a list of items and then display that list in the GUI. Because the GUI can only be accessed through the OnGUI function, I had to play around a little and try to figure a way to do this. I came up with using an Array to store the values, parsing that to a string inside the OnGUI function and then popping that string into the GUI, a GUI.box specifically. The problem is that I need to alter the array from another piece of script and I cannot seem to get the other script to find the Array. In script A I declared the Array in this way

static var thelist:Array;

Obviously, that is in Java. The name of the script is “updater” and the rest of it works fine, so no other issues there.

#pragma strict
static var thelist:Array;
static var slist:String;

function Start(){
slist=slist+"cube";
}

function OnGUI () {
for (var temp in thelist){
slist=slist+temp;
}
GUI.Box(Rect(0,0,150,150), slist);

}

From script B, which is called “destroyit”, I have tried to access “thelist” several ways and get nothing but errors. From the docs I see that you should use the script name as the base class, but “updater.thelist” brings an error stating that updater cannot be found. A direct call to “thelist” brings the same type of error. I also noticed that in the Unity inspector the above script shows “slist” as a Global variable, but does not show “thelist.” I find that a little strange since both are declared in an identical manner. So, I am really looking to understand how to properly declare and access a Global variable. I also prefer C#, but most of the forum docs used Javascript, so I used that language for these two scripts.

Accessing other scripts. Also, don’t use Array. Use built-in arrays or List.

From where are you trying to access your variables?
If from C#, it is possible it is compiled after your js script thus fails to know it.

http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29

Array can not be shown on inspector even when it is public.

In JS though you need to declare the as a Built in array:

  public var mylist: string[];

You should see that in the inspector.

Access it via:

 GameObject.Find("GameObjectGUI").GetComponent(myScript).mylist[n]

Where “GameObjectGUI” is the object name that has the script and “myScript” is the script with the array.