- Home /
How to assign different Components to one array
Hi
Is it possible to create a Built-in array which contains several classes(MonoBehaviours)? I'm trying to solve this for quite some time now and can't find a solution for this :(
Here is my pseudo Code which shows what I would like to do.
#pragma strict
var behaviourClasses : ???;
function test() {
behaviourClasses = new ???[2]
behaviourClasses[0] = GetComponent(Attack);
behaviourClasses[1] = GetComponent(RunAway);
for ( var i: int = 0; i < behaviourClasses.length; i++) {
if ( behaviourClasses[i].Ready() ) {
behaviourClasses[i].ready = true;
behaviourClasses[i].Run()
break;
}
}
}
Does someone know how to do this?
Answer by Em3rgency · Jun 23, 2013 at 08:17 PM
That's the thing, you can't. An array can only store one type of variable. Different kinds of data under one roof is a class. The whole point of classes is that they store many different types of variables and methods that all combine into a singular purpose.
Now what you could do is a master class, and then having all your other classes be children of that class. If your classes have enough similarities between them, that you could place into the parent class instead, and have them inherited, it would work.
As an example unity's GameObject class is a parent to all game scene entities.
I've tried to create one $$anonymous$$aster class which contains virtual functions and then override them with different behaviour classes. But I can't get it to work. I always get errors like 'You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword.' Can you maybe show me an example code how to do this?
here is my Class based attempt so far
// BehaviourClass.js
#pragma strict
var behaviourClasses : BehaviourClass[];
function Start() {
behaviourClasses = new BehaviourClass[1];
behaviourClasses[0] = new ChaseAndAttack();
// testing
behaviourClasses[0].Check();
// behaviourClasses[0].Run();
// print( behaviourClasses[0].varTest );
}
function Check () {
Debug.Log("Test 1");
}
function Run () : IEnumerator {
Debug.Log("RunTest 2");
}
and in another file ...
// ChaseAndAttack.js
#pragma strict
class ChaseAndAttack extends BehaviourClass
{
public var varTest:String = "chase and attack variable";
override function Check() {
Debug.Log ("Chase and Atack print");
}
override function Run() {
Debug.Log ("RunTest before yield");
yield;
yield WaitForSeconds(1);
Debug.Log ("RunTest after yield");
}
}
The second file looks ok, that IS how you create child classes, with extends.
But the first file... You're still trying to make an array of classes. And then store another class into... wha? Forget arrays all together.
Your ChaseAndAttack class already has all the methods from BehaviourClass, so as long as you make an object of ChaseAndAttack, you will be able to use all the methods inside.
the thing is, I need a function like this >
for ( var i: int = 0; i < behaviourClasses.length; i++) {
if ( behaviourClasses[i].Ready() ) {
behaviourClasses[i].ready = true;
behaviourClasses[i].Run()
break;
}
}
I need something like this to check which ai behavior should get started next. I'll have many different Ai behaviors like ChaseAndAttack, RunAway, WalkAround and every behavior is separated in a separate file. every behavior has the same functions (Ready()/Run()). The Script checks if Ready() returns true and if so, it will start Run(), if not it will check the next behaviour for Ready() and so on...
Unfortunately I don't know a different way how to do this :\
You can't new a $$anonymous$$onoBehaviour (as the error tells you). That's the only problem. You have to AddComponent the behaviour or use a non-behaviour class.
Dude, this IS basic stuff. He creates an object of class DisplayText. The class type is Behavior.
EDIT: sorry, I'm tired, pulled an all nighter. Classes don't have types, but the class inherits from Behavior.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
accessing vars of other script doesn't work 1 Answer
Get variable from Script in list and , set direct link to other script 1 Answer
How transform Variable is accessed in Scripts ? 2 Answers
script needs to derive from monobehaviour 1 Answer
Monobehavior class variable become null after assigning value when instantiated 1 Answer