Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 yosoycody · May 06, 2013 at 03:08 PM · c#animationgameobject

How can I animate between 3 gameObjects?

I have 3 gameObjects, all the with the same tag, and each have many children. The first one starts out as visible in the game, while the second and third are initially hidden/de-activated (by un-checking the box in the inspector).

When it gets collided into, I need it to hide, show the second gameObject in its place, then hide the second gameObject, and show the third gameObject, then hide the third gameObject, show the second gameObject, then hide the second gameObject, and show the first gameObject again.

The problem I'm running into is I'm trying to do this in a coroutine and grabbing the 3 gameObjects like so: GameObject[] myObjects = GameObject.FindGameObjectsWithTag("myTag");

But it's only finding the one that's not hidden.

Any ideas on how to animate between these 3 objects?

Thanks!

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
0
Best Answer

Answer by Scribe · May 06, 2013 at 04:35 PM

You are correct that .Find() will not work on inactive GameObjects so the easiest way to work round this is to saved the objects at the start into a list and then deactivate them afterwards by code.

So make sure you have all the objects activated to start with and then use:

 GameObject[] myObjects = GameObject.FindGameObjectsWithTag("myTag");

in a 'Start' function

Here is some C# code as it looks like that is what you are using, however I will disclaim that I am very bad at C# and tend to write in unityscript when using Unity (I have therefore also included the unityscript code below incase there are errors in the C# code)


C# version


 using UnityEngine;
 using System.Collections;
 
 public class find : MonoBehaviour {
     GameObject[] myObjects;
 
     void Start () {
         myObjects = GameObject.FindGameObjectsWithTag("myTag");
         for(var a = 0; a < myObjects.Length; a++){
             myObjects[a].active = false;
         }
         myObjects[0].active = true;
     }
     
     void OnTriggerEnter (Collider other) {
         for(var i = 0; i < myObjects.Length; i++){
             if(other.gameObject == myObjects[i]){
                 myObjects[i].active = false;
                 myObjects[((i+1)%myObjects.Length)].active = true;
             }
         }
     }
 }



Unityscript version


 var myObjects : GameObject[];
 
 function Start () {
     myObjects = GameObject.FindGameObjectsWithTag("myTag");
     for(var obj in myObjects){
         obj.active = false;
     }
     myObjects[0].active = true;
 }
 
 function OnTriggerEnter (other : Collider) {
     for(var i = 0; i < myObjects.Length; i++){
         if(other.gameObject == myObjects[i]){
             myObjects[i].active = false;
             myObjects[((i+1)%myObjects.Length)].active = true;
         }
     }
 }



I hope that works for you, remember to have all 3 objects active when the scene starts else it will not find the objects as you have found out.

Scribe

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

13 People are following this question.

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

Related Questions

C# Make Child Not Animate Looped Animation 1 Answer

adding animation dynamically by script 0 Answers

Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers

C# Adding Components From Other Gameobjects 3 Answers

How to apply a shader to all materials inside a gameobject 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