Instantiating gameobject variables (in an array) from a different script

I have 2 prefabs assigned to variables (placed in an array) in a script called test:

var one : GameObject;
var two : GameObject;

static var prefabs = new Array();
prefabs[0]= one;
prefabs[1]= two;

I’m trying to access them with another script as follows:

function Start () {
var instance : GameObject = Instantiate(test.prefabs[0], transform.position, transform.rotation);
}

…but I’m having no luck - the error in the console is “ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.”

Can anyone explain what I’m doing wrong?

When you want to use a variable from another script, you have to specify that script first. However, the compiler cannot understand the script that you’re trying to “inspect” on its own.

I’m supposing that the first script that you’ve posted is called “script1”, the second one is called “script2”, and script1 is assigned to an object called “my_object”. So, try this (in script2):

function Start(){
   var my_prefab=GameObject.Find("my_object").GetComponent(script1).prefabs[0];
   var instance=Instantiate(my_prefab, transform.position, transform.rotation);
}