Accessing arrays across scripts in js

I am trying to access an array element in one script file from another. For example there are two script files Move1.js and Move2.js.

Example:to access the second element in an arrayP in a script called "Move1" Move1.arrayP[1];

This seems to work for global variables but I am looking for a solution to use with arrays, can anybody shed some light on the mechanism or syntax to do this in js, thanks for helping.

1 Answer

1

There aren't really "global" variables as your script is a class and the variables are members of that class, but there are static variables that are shared by a class. If you are using non-static variables, you will need an instance of the class to get the instance of the variable from. As long as your variables are public, you should be able to access them.

Arrays are variables too.

script1.js

//This is a static array.
static var foo : Transform[];
//This is an array instance
var bar : Transform[];

script2.js

var something : GameObject;

function Start() {
    script1.foo.push(transform); //static variable instances are known
    if(something) {
        //get an instance of the script's class attached to something
        var script : script1 = something.GetComponent(script1);
        if(script) script.bar.push(transform);
    }
}