Iterate through inherited gameobjects

Hi,

I have 2 problems problem:

  1. I don’t know how to put code in a better way (as I’ve seen in other questions).

EDIT: FIRST PROBLEM SOLVED!

  1. I have a class that inerits from monobehaviour “Base”, and I have 2 other subclasses that inherits from this base class “Child1” and “Child2”. Both of them have a method “Act”

    public class Base : Monobehaviour {
    public Act() {
    }
    }

On a gameobject1 I use the Child1 derived class

public class Child1 : Base{
     public Act() {
         //do something
     }
 }

On another gameObject2 I use the Child2 derived class

public class Child2 : Base{
     public Act() {
         //do something else
     }
 }

My idea is that I have a AI object that commands this game objects like this:

GameObject[] arrayGO;  // here I have the reference of both objects
foreach (gameobject g in arrayGO) {
    g.GetComponent<"here is my problem">().Act();
}

my probles is exactly that, since both child has different scripts, I don’t know to make this simple, mostly because I want to make at least 10 different types of childs from base class.

Any suggestions how to do this?

Cheers!

// This does not work
g.GetComponent().Act();

How does it break for you ?

I tested with this setup and it works perfectly fine

// Test.cs - attached to Camera in my scene
public class Test : MonoBehaviour {
	// dragged 2 scene gameobjects containing scripts Child1 & Child2 into this array in inspector
	public GameObject[] _Objects; 
	void Start () 
	{
		foreach (GameObject g in _Objects) 
		{
			g.GetComponent<Base>().print();
		}
	}

	//prints out
	// i'm 1
	// i'm 2
}
// Base.cs
public class Base : MonoBehaviour {
	public virtual void print()
	{
		Debug.Log ("i'm base");	
	}
}
// Child1.cs - attached to an empty GameObject in my scene
public class Child1 : Base {
	public override void print()
	{
		Debug.Log ("i'm 1");	
	}
}
 // Child2.cs - attached to an empty GameObject in my scene
public class Child2 : Base {
	public override void print()
	{
		Debug.Log ("i'm 2");	
	}
}

I am pretty sure you can use the feature GameObject.SendMessage() that is part of unity for this.

GameObject[] arrayGO;  // here I have the reference of both objects
foreach (gameobject g in arrayGO) 
{
     g.SendMessage("act");
}

The gameobject.sendmessage(“methodtocall”) accesses all classes attached to the game object irrelevant of type and calls the input method. This should fit perfectly with your problem.

here is an example of its implementation in the unity docs:

--------------------------Method2------------------------------------------------------------------------

You could attach a helper class to each game object that can handle calling of the act method ambigiously
e.g.

 GameObject[] arrayGO;  // here I have the reference of both objects
  foreach (gameobject g in arrayGO) 
  {
     g.GetComponent<helper>().act();
  }

Example helper class

    public class helper
    {
        // behaviour type will be used to call the different act method in each behaviourtype
        public string behaviourtype;

        public void act()
        {
           invoke(behaviourtype);
        }

        public void callbehaviourtype1()
        {
           gameobject.Getcomponent<behaviourtype1>().act();
        }
        public void callbehaviourtype2()
        {
           gameobject.Getcomponent<behaviourtype2>().act();
        }
    }