- Home /
How to get a value from an array within another script.
Ok, so I got a script attached to a gameObject, lets say the name is objectProperties, then I have another script on the mainCamera called theGUI, which also creates my gui that should fetch the variable in an array from the gameobject tagged with 'selected'. So I got the following:
objectProperties:
private var code : int[];
code = new int[11];
function Start() {
for(n = 0; n < 12; n++) {
code[n] = 0;
}
}
theGUI:
private var code : int[];
code = new int[11];
function Update() {
if(gameObject.FindGameObjectWithTag("selected") != null) {
var objectProperties : GameObject = gameObjectFindGameObjectWithTag("selected");
for(var n = 0; n < 12; n++) {
code[n] = objectProperties.GetComponent(objectProperties).code[n];
}
}
}
Now I want it to take the variable from the script objectProperties, attached to the current gameObject with the tag selected, to the script theGUI, attached to the mainCamera. While, I could set the variables from objectProperties, within the GUI, so that's no problem.
Answer by Henrique Vilela · May 23, 2011 at 07:59 PM
You're code variable at objectProperties must be public to be visible from the other script.
public var code : int[];
Ok, but I still get 3 errors. - BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found. BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'. BCE0049: Expression 'objectProperties.GetComponent(objectProperties).code[0]' cannot be assigned to.