Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tylerlybb · Oct 17, 2016 at 11:47 AM · c#animatoranimator controllerchildren

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);
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tylerlybb · Oct 17, 2016 at 11:39 AM 0
Share

Oh, I forgot to mention that I'm using a UI button to trigger "AnimateNowFuntion" on Parent.

avatar image tylerlybb · Oct 18, 2016 at 01:34 AM 0
Share

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!

avatar image TBruce tylerlybb · Oct 18, 2016 at 01:46 AM 1
Share

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.

avatar image tylerlybb TBruce · Oct 18, 2016 at 01:59 AM 0
Share

Will do. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);        
             }
       }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tylerlybb · Oct 19, 2016 at 02:41 AM 0
Share

I tried your code and it doesn't work. If you have any ideas. But thank you so much for helping me!

avatar image tylerlybb · Oct 19, 2016 at 03:29 AM 0
Share

When I use your code exactly I get this error:

Assets/AnimateNow.cs(16,40): error CS1041: Identifier expected

avatar image red-sight tylerlybb · Oct 19, 2016 at 10:35 AM 0
Share

Cool I've updated it, there was a syntax error

avatar image tylerlybb red-sight · Oct 19, 2016 at 11:21 AM 0
Share

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".

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to animate a chest opening and closing, but the opening and closing animations aren't playing when they should. 1 Answer

Animating in c# 1 Answer

Editing other object's animator parameter... NullReferenceException? (VR) 0 Answers

MobController 1 Answer

AnimationOverrideController does not set clip. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges