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 MCHammond · Dec 17, 2012 at 04:39 PM · coroutineoptimizationforeach

StartCoroutine for each object in a foreach() Loop

I am having some issues getting my head around the Coroutine concept in C#

Question

I have a list of objects that I want to manipulate via a script but I want that action to be preformed a frame latter for each consecutive object so as to reduce the load on the CPU.

My sudo code is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     void MainAction(GameObject[] TargetList){
         int Loop = 0;
         foreach (GameObject Target in TargetList){
             StartCoroutine(Action(Loop,Target));
             Loop++;
         }
     }
     
     IEnumerator Action(int Loop,GameObject Target){
         yield return new WaitForSeconds(Loop/2f);
         Target.transform.localScale = new Vector3(0,0,0);
     }
     
     void Update(){
         GameObject[] TargetList = GameObject.FindGameObjectsWithTag("Target");
         MainAction(TargetList);
     }
 }

Any help would be really appreciated!

Comment
Add comment · Show 2
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 DannyB · Dec 17, 2012 at 04:48 PM 0
Share

From your pseudo code it seems that each frame you will run the main action, which (still in each frame) would loop through all the objects and execute your coroutines. Is this what you meant to do? I mean this could be fine, if something clears your target list immediately after it was sent to the main action method.

avatar image MCHammond · Dec 17, 2012 at 04:57 PM 0
Share

Thanks for the help, I am gona have to look at my main code a bit more as this sudo code seems to work fine.

1 Reply

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

Answer by MCHammond · Dec 17, 2012 at 04:54 PM

Hmm...

I just used my Sudo code in a fresh project and it seems to work :/ I guess that there is an issue with my main code other than my understanding of Coroutine.

I have updated My original Sudo Code with the Code I used in my blank Project that works so that others searching for an example of Coroutine code can see it.

I have marked this as correct because my question answers itself and the updated Sudo code does what I wanted it to do.

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

10 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

Related Questions

Vector3.Lerp curving along three destinations 1 Answer

Calculating the Exact Center of Multiple Objects 2 Answers

Load Children One at a Time 1 Answer

C# while within foreach doesn't complete 2 Answers

How do I do something after multiple coroutines finish? 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