• 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 Thomas-Hawk · Jun 25, 2018 at 06:04 AM · coroutineienumeratorstartcoroutineiterategarbage collection

Can I use StartCoroutine inside of the same coroutine with a new variable?

I have a couroutine which takes a class of mine, "Wire", as a parameter.

This coroutine, changes a boolean state on this wire class, and then calls the same coroutine on the 1-5 other wires stored in its class instance as a list to also change their state (if, of course, they need to be changed, to avoid an infinite back-and-forth loop)....which then of course calls the same coroutine passing those, etc etc.

Does this count as garbage? Is there a way to trigger that coroutine without it being nested? Am I correct to say that, this means, the coroutine called on the first wire is not stopping until the last wire in the chain's coroutine stops?

alt text

Here is the coroutine in question. "pulser" is just taking a single reference to a single wire object, and calling this coroutine, passing that wire....that wire, in this snapshot of the profiler, is connected to a line of other wires.

     public IEnumerator RefreshOneSimple(WireNode theNode){

         WireNode temp;
 
         //For every wire this one is connected to,
         foreach (Behaviour conn in theNode.ports) {
             if (conn != null) {
                 if (conn.name.Contains ("WireNode")) {
                     temp = conn as WireNode;
                     if (temp.state != theNode.state) {
                         //If it is not the same state as this one, change that now.
                         temp.state = theNode.state;
                         foreach (LineRenderer cable in theNode.wirelines) {
                             if (theNode.state == true)
                                 cable.materials [0].color = theNode.onColor;
                             if (theNode.state == false)
                                 cable.materials [0].color = theNode.offColor;
 
                         }
                         foreach (LineRenderer cable in temp.wirelines) {
                             if (temp.state == true)
                                 cable.materials [0].color = theNode.onColor;
                             if (temp.state == false)
                                 cable.materials [0].color = theNode.offColor;
 
                         }
                         //yield return null;
                         StartCoroutine (RefreshOneSimple (temp));
                         yield return null;
                     }
 
 
                     //Now do the same thing with each one of those, and then let the next frame happen. 
 
                 }
 

                 if (conn.name.Contains ("Powerable")) {
                     //print ("Doing powerable in enum");
                     Powerable p = conn as Powerable;
                     p.powered = theNode.state;

                     p.StartRefresh ();
                     yield return null;
 
 
                 }
 
             } else {

             }
         }
 

         yield return null;
     }


EDIT: I thought, well, if instead of calling this routine inside of itself, I could call a new routine which simply starts the original routine with the new variable, then once that new "StartCoroutine" exits, the first one can disappear.

But, this resulted in, only my first wire updating. So I reverted.

Edit: In case I am not clear, I only need any of this to happen when a wire has its state changed. I.e., during the moment a lever is switched.

coroo.png (46.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

93 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Item duration doesn't stack 3 Answers

Coroutines IEnumerator not working as expected 2 Answers

Is there a way to show a warning when a Coroutine is called directly? 0 Answers

How to pass "ref parameter" in iterator method? 1 Answer

Coroutine won't stop running. Tried String. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges