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 /
avatar image
0
Question by Heldenwelt · Oct 26, 2015 at 05:04 PM · animationanimatorscript.colliders

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");
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");
         }
     }
 }
Comment
Add comment · Show 2 · 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 Heldenwelt · Oct 27, 2015 at 09:38 AM 0
Share

Thanks a lot The little Guy!

It works fine. One little letter "s". Unbelievable. :)

avatar image tanziying2019 · Mar 22, 2020 at 10:02 PM 0
Share

This works for me as well! $$anonymous$$illion thanks! You saved my life!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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