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 artfish · Mar 23, 2015 at 09:28 AM · coroutinelooparray of gameobjects

How do I activate game objects in array in timed loop?

I have a scene with an array containing 10 objects. I want to loop through each game object, activate it for 20 seconds, then deactivate it and then do it for the next object in the array. I want it to basically keep looping through the game objects as long as a bool is true.

I've been googling a lot and scouring through answers. I'm pretty sure that I'll want to use a coroutine, but not quite sure how I can pull this off.

Logic-wise, basically I want to do this.

-While attractorMode=true - set gameobject1 active - wait 20 seconds - set gameobject1 to inactive - set gameobject2 active - wait 20 seconds - set gameobject2 to inactive

I want repeat for all 10 objects and then restart back with gameobject1 once all 10 have finished.

Thanks for any tips, code or just pointing me in the right direction.

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
4

Answer by AlwaysSunny · Mar 23, 2015 at 09:28 AM

From your description, it's clear that you must be cautious about invoking your coroutine, such that it is only running one instance at a time.

When your bool becomes false, do you want the next pending transition to occur, or not occur? The following code will execute the pending change, even once you set attractorMode to false. This can be corrected if it's not what you want, but it's up to you to figure out how. :D

Hopefully this'll give you some insight into how coroutines work. Your request is just potentially-confusing enough to warrant a demonstration script, but don't expect free scripts too often. ;)

 bool attractorMode, isRunning;
 int currentIndex;
 
 void Update() {
   // only invoke if the coroutine isn't already running
   if (attractorMode && !isRunning) StartCoroutine( Cycle() );
 }
 
 IEnumerator Cycle() {
   // these lines execute by the end of the frame in which Cycle() was invoked
   isRunning = true;
   objects[currentIndex].SetActive(true);
   yield return new WaitForSeconds(20);

   // execution will not return to the next statement until 20 seconds later    

   objects[currentIndex].SetActive(false); 

   // increment the index and ensure it loops around at the end
   currentIndex++;
   if (currentIndex >= objects.Length) currentIndex = 0;

   isRunning = false;
 }




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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

why does my current code line consist next line of code information 1 Answer

Stop Current Coroutine, Then Restart It 1 Answer

Wave Spawning not working 1 Answer

Coroutine not waiting for While Loop to finish 1 Answer

While loop yield lag? 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