How to assign different Components to one array


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 :frowning:

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*.Ready() ) {*

_ behaviourClasses*.ready = true;_
_ behaviourClasses.Run()
Does someone know how to do this?_

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.

Since you needed to add them to an Array,
use the Array class, check it here and leave comments if you face problems.