Access a lot of child Animators at once!

Hi Unity Fans,

I have an empty GameObject with a Trigger collider. Under the Game Object I have 200 Objects with an animator. Now I want to start the animation by entering the collider. It works so far, but it only starts the first child object in the row. The work arround would be 200 colliders (every object) :slight_smile: ! But that would be horrible.
Is there a way? Here is the code. Maybe there is some think like: Look for all children or something?

Thanks.

using UnityEngine;
using System.Collections;

public class Trigger_Animator_Checker_child : MonoBehaviour {

    Animator animator;
	// Use this for initialization
	void Start () {

        animator = GetComponentInChildren<Animator>();
}
	// Update is called once per frame
	void Update () {
	}

    void OnTriggerEnter(Collider col)
    {
        animator.SetTrigger("StartCube");
    }
    void OnTriggerExit(Collider col)
    {
        animator.SetTrigger("StopCube");
    }
}

I think you are looking for GetComponentsInChildren instead. This returns an array of components. You can then loop through them and do whatever it is that you are looking to do.

using UnityEngine;
using System.Collections;

public class Trigger_Animator_Checker_child : MonoBehaviour {

    Animator[] animators;
    // Use this for initialization
    void Start () {
        animators = GetComponentsInChildren<Animator>();
    }

    void OnTriggerEnter(Collider col)
    {
        foreach (Animator animator in animators)
        {
            animator.SetTrigger("StartCube");
        }
    }
    void OnTriggerExit(Collider col)
    {
        foreach(Animator animator in animators)
        {
            animator.SetTrigger("StopCube");
        }
    }
}