Animation only plays in one of the objects
Hello.. I have an script when I click it will play an animation, I have cloned the Game Objects that have the animation, but for some reason only ONE of the objects will play the animation.
The object "Beacon" it's the one with the animation, I have them with a tag "Beacon"
GameObject.FindWithTag("Beacon").GetComponent<Animation>().Play();
That is the line of script that calls the animation, but for some reason only one of the objects plays the animation the other two don't do anything.
thank you (and sorry for bad english)
Answer by UnityCoach · May 04, 2017 at 07:07 PM
Hi,
this line tells to find the first object with the tag "Beacon". You can use
GameObject[] obj = GameObject.FindGameObjectsWithTag ("Beacon");
if (obj.Length > 0)
{
for (int i = 0 ; i < obj.Length ; i++)
{
Animation a = obj[i].GetComponent<Animation>();
if (a)
a.Play();
}
}
That said, it would be better to use this, if you don't have other Animation within the hierarchy.
Animation[] animations = GetComponentsInChildren<Animation>();
if (animations.Length > 0)
for (int i = 0 ; i < animations.Length ; i++)
animations[i].Play();
O$$anonymous$$G THAN$$anonymous$$ YOU, IT'S FINALLY WOR$$anonymous$$ING :D