- Home /
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) :) ! 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");
}
}
Answer by The-Little-Guy · Oct 26, 2015 at 05:28 PM
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.
http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
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");
}
}
}
Thanks a lot The little Guy!
It works fine. One little letter "s". Unbelievable. :)
This works for me as well! $$anonymous$$illion thanks! You saved my life!
Your answer
Follow this Question
Related Questions
What's the difference between animation and animator ? 0 Answers
How to play animation when player is moving? 2 Answers
How to check if Animator is playing 3 Answers
I want to give animation order to multiple objects in a single script. How can i do? 0 Answers
[Solvedf]Animator is not playing an AnimatorController 2 Answers