If this helps anyone, i got my code working. I can have public static array of class types and i can even modify their value in inspector. Here is code:
BuildManager.js
#pragma strict
var Gobj : GObject[];
public static var GameObjects : GObject[];
function Start()
{
var Count : int = 0;
GameObjects = new GObject[Gobj.length];
for (i in Gobj)
{
print(i.name);
GameObjects[Count] = i;
Count++;
}
}
And this is how you acces it from other script :
unction OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,100,90), "Build menu");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (20,40,80,20), "Choose building")) {
print(BuildManager.GameObjects[0].name);
}
}
You typically use GetComponent to access public variables from other scripts. I wouldn’t recommend using a static variable like that, because then you have duplicate arrays for no good reason.
Using GetComponent will have much higher function call times than straight array access. Having two arrays isn’t really problem, and they are filled on start of program so it dosent impact perfomance !
Not really; if you’re using GetComponent that frequently then you’d cache the call, so performance wouldn’t be an issue. Having two separate arrays and copying the contents is just making things a lot messier than they should be.