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 rickymanalo · Jul 27, 2020 at 03:40 PM · coroutines

How to stop a single variant of a Coroutine?

I'm making a Coroutine helper class that would handle everything related to Coroutine. In that helper, I have a switch where 3 cases would start the same IEnumerator. I've seen that you can store the return of StartCoroutine but my problem is that, even though it's the same IEnumerator, their attributes are different from each other. To give an example, imagine I need to change a color of something using Coroutine. I would create a single IEnumerator where I would pass the color. Then for example I started a IEnumerator for yellow, red, and green. What if I wanted to stop red?

I tried the suggestion of @andrew-lukasik but have to make some changes to make it "work". Like using List instead of a Dictionary. I ended up using a List of a struct that contains a ScriptableObject and a IEnumerator. My problem now is that StopCoroutine(IEnumerator); doesn't stop the IEnumerator for some reason.

Comment
Add comment · Show 3
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 huseyinbaba58 · Jul 27, 2020 at 04:03 PM 0
Share

You should use Switch Case for whole staues.I guess like this

avatar image andrew-lukasik · Jul 27, 2020 at 05:14 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class DuoBehaviour : $$anonymous$$onoBehaviour
 {
     Dictionary<string,IEnumerator> _routines = new Dictionary<string,IEnumerator>(10);
     public Coroutine StartCoroutineWithID ( IEnumerator routine , string id )
     {
         var coroutine = StartCoroutine( routine );
         if( !_routines.ContainsKey(id) ) _routines.Add( id , routine );
         else
         {
             StopCoroutine( _routines[id] );
             _routines[id] = routine;
         }
         return coroutine;
     }
     public void StopCoroutineWithID ( string id )
     {
         if( _routines.TryGetValue(id,out var routine) )
         {
             StopCoroutine( routine );
             _routines.Remove( id );
         }
         else Debug.LogWarning($"coroutine '{id}' not found");
     }
 }

 StartCoroutineWithID( $$anonymous$$akeSomething(Color.red) , "make it red" );
 StartCoroutineWithID( $$anonymous$$akeSomething(Color.green) , "make it green" );
 // milliseconds later...
 StopCoroutineWithID( "make it red" );
avatar image rickymanalo andrew-lukasik · Jul 28, 2020 at 11:15 AM 0
Share

seems like this would work. will try it

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jul 27, 2020 at 04:43 PM

Each coroutine or IEnumerator instance is a seperate unique object in memory. There is no way to distinguish them. So if you want to stop a certain coroutine, you have to remember the object you've created. Either the IEnumerator or the corresponding Coroutine object.


Maybe reading through my coroutine explaination which I wrote recently might help to better understand how they actually work.

Comment
Add comment · Show 1 · 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 rickymanalo · Jul 28, 2020 at 11:14 AM 0
Share

Can't really say that I need all that information right now, but it would be really useful in the future. Thanks

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

134 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 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

WaitForSeconds is going too fast 1 Answer

WWW download in background during position animation 2 Answers

Time.time undependable in a coroutine? 1 Answer

Dynamically updating a Coroutine's parameters every frame 1 Answer

How to stop a coroutine with multiple parameters? 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