C# to set trigger on animator components of all children
I know this is probably simple. Can someone give me some C# example code for how to do the following:
I have a game object acting as a container for two cubes (call it "Parent"). Each of the cubes has an animator component on it, each using the same animator controller. That controller uses the parameter "AnimateNowParam" to transition from one state to another. I have the following script on Parent. But it doesn't cause both cubes to changes animation states. It only causes the first cube to change animation states. How do I get it to cause both cubes to change states?
using UnityEngine;
using System.Collections;
public class AnimateNow : MonoBehaviour
{
Animator anim;
int buttonHash = Animator.StringToHash("AnimateNowParam");
void Start ()
{
anim = GetComponentInChildren<Animator>();
}
public void AnimateNowFunction()
{
anim.SetTrigger (buttonHash);
}
}
Oh, I forgot to mention that I'm using a UI button to trigger "AnimateNowFuntion" on Parent.
Thank you thank you! However, it doesn't work and I'm having a hard time figuring out why. If you have any follow-up advice I will thank you again in all CAPS!
When you receive an answer from someone that helps you out, make sure to accept it by clicking on the “Accept” button. When you do this, both you and the person that answered your question earns karmapoints which helps boost ones reputation.
Also, if the answer was really helpful, Upvote them.
Answer by red-sight · Oct 17, 2016 at 01:16 PM
GetComponentsInChildren is what you want: https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
This will return an array that you can either iterate over with a for loop or use forEach with a delegate method.
I've not ran this but it should be close:
using UnityEngine;
using System.Collections;
public class AnimateNow : MonoBehaviour
{
Animator[] anims;
int buttonHash = Animator.StringToHash("AnimateNowParam");
void Start ()
{
anims = GetComponentsInChildren<Animator>();
}
public void AnimateNowFunction()
{
foreach (Animator anim in anims )
{
anim.SetTrigger (buttonHash);
}
}
I tried your code and it doesn't work. If you have any ideas. But thank you so much for helping me!
When I use your code exactly I get this error:
Assets/AnimateNow.cs(16,40): error CS1041: Identifier expected
Cool I've updated it, there was a syntax error
Thank you, but I still get an error, though a different one:
Assets/AnimateNow.cs(79,5): error CS8025: Parsing error
Could it be that when it gets to the looped code ...
anim.SetTrigger(buttonHash);
... it doesn't know what "anim" is? With my very limited knowledge of C# it seems like maybe we haven't told it what "anim" is, only "anims".
Answer by tylerlybb · Oct 18, 2016 at 02:00 AM
I tried your code and it doesn't work. If you have any ideas. But thank you so much for helping me!
Your answer
Follow this Question
Related Questions
Animating in c# 1 Answer
Editing other object's animator parameter... NullReferenceException? (VR) 0 Answers
MobController 1 Answer