- Home /
In an array of scripts, how to access their variables?
I've created an array of scripts all attached to the same game object and deactivated. What I'd like to do is to be able to pick three random members of the array and activate those scripts, drawing upon the variables within. However, I run into either a typing error at the outset (thus the untyped declaration here), or, when attempting to enable the script or access a variable, the error that either 'enabled' or 'foo' is not a member of 'Object'. Is there a way to do this cleanly?
var myArray : Array = new Array();
function Start(){
myArray[0] = gameObject.GetComponent(Script0);
myArray[1] = gameObject.GetComponent(Script1);
myArray[2] = gameObject.GetComponent(Script2);
myArray[3] = gameObject.GetComponent(Script3);
myArray[4] = gameObject.GetComponent(Script4);
myArray[5] = gameObject.GetComponent(Script5);
myArray[1].enabled = true;
}
function Update (){
Debug.Log(myArray[1].foo);
}
They do! Just two variables that each script has its own instance of.
Ah, thank you to the both of you. Worlds to explore, here. Now, this might warrant a different question entirely, or perhaps I've led us down the wrong path, but does this construction eli$$anonymous$$ate the possibility that each of my three active scripts can provide a different value for the common variable names? Or, in bringing them all together, do they now share a single instance (i.e. the most recently updated)?
Each script would be its own instance. BaseScript only means that all of your Script* scripts are of the same base type and thus share an implementation, but it doesn't mean they share the same set of instance variables. So, yes, each script would provide a different value for the common variable names.
I would encourage taking a look at the following links to get a head start on using UnityScript.
http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long) http://wiki.unity3d.com/index.php?title=Head_First_into_Unity_with_UnityScript http://forum.unity3d.com/threads/121178-UnityScript-Inheritance-and-Constructors http://answers.unity3d.com/questions/279809/how-to-perform-explicit-typecasting-in-unityscript.html
Hope this helps.
I see. I was feeling pretty confident, but clearly I have to do some reading into inheritance and class definition. I will take your links to heart. Thanks again!
Answer by ArkaneX · Oct 09, 2013 at 09:52 PM
If, according to comment, all the scripts have common variables, then create a BaseScript class derived from MonoBehaviour, with your variables. And then make all your scripts derive from this class.
Additionally, don't use Array class. Use either strongly typed array or generic list. For above base class, this would be:
var myArray : BaseScript = new BaseScript[6]; // you have to declare size
or
var myList : List.<BaseScript> = new List.<BaseScript>(); // requires importing System.Collections.Generic namespace
If you use list, then you can add elements to it using
myList.Add(gameObject.GetComponent(Script0));
Following your instructions as best I can (and using list), I keep getting the following error:
The best overload for the method 'System.Collections.Generic.List.<BaseScript>.Add(BaseScript)' is not compatible with the argument list '(Script0)
I should disclose that I'm completely new to creating my own classes. Somehow I suspect that this is insufficient:
class BaseScript{
var foo: boolean;
var bar: GameObject;
}
$$anonymous$$ake class BaseScript inherit from $$anonymous$$onoBehaviour:
class BaseScript extends $$anonymous$$onoBehaviour { ...
Then make each Script* inherit from BaseScript:
class Script0 extends BaseScript {...} class Script1 extends BaseScript {...}
class BaseScript extends $$anonymous$$onoBehaviour
{
var foo: boolean;
var bar: GameObject;
}
and then you have to derive your normal scripts from this base class:
class Script0 extends BaseScript
{
// content of your script
}
EDIT: I see Rocket Alien was much faster :)
Answer by $$anonymous$$ · Oct 09, 2013 at 09:40 PM
Casting your random script instance to a MonoBehaviour may help as each script by default is a MonoBehaviour.
var script = myArray[1] as MonoBehaviour; script.enabled = true;
Is 'foo' a member of all your scripts? You may need to create a base script and make all your scripts inherit from it. Then, cast to the base script type when you access 'foo'.