• 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 Victor Fabiano · Nov 23, 2014 at 05:13 PM · coroutinerun-time

How to instantly restart a coroutine?

I was wondering to know how to make a coroutine instantly stop and run again (restart).

For example: I'm running a coroutine that smoothly turns alpha from 255 to 0. Before it ends, for example, in the iteration 125, some action happens and this value goes to 255 again and start fading alpha from 255 to 0 from the start. Keep in mind this coroutine will instantly stop just in the time an event occurs, and will start again, in our case from 255 and will fade to 0.

I'm trying to be the clearer I can, if I wasn't, ask me and I'll try to explain again.

Thank in advice and sorry for my bad English.

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

Answer by Jignesh G. · Nov 23, 2014 at 06:06 PM

First, why you using co-routine of fading affect.? the simplest way is to use iTween.Value to to achieve color fade effect.

 public float fromValue = 0;
  
  private IEnumerator valueToExample()
  {
       iTween.ValueTo( "from", fromValue, "to", 10, "onupdatetarget", gameObject, "onupdate", updateFromValue, "time", 5, "easetype", iTween.EaseType.easeOutExpo );
  }
  
  public void updateFromValue( float newValue )
  {
       fromValue = newValue;
       Debug.Log( "My Value that is tweening: " + fromValue );
  }


Ulternate Way :

Call IEnumerator like below which can be stopped and start again.

 void Start () 
     {
         StopCoroutine ("fadeTo");
         StartCoroutine ("fadeTo");
 
     }
 
     IEnumerator fadeTo()
     {
 
         //some code goes here
         yield return new WaitForSeconds(1F);
     }

Comment
Add comment · Show 4 · 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 Victor Fabiano · Nov 23, 2014 at 06:29 PM 0
Share

Sorry... iTween? I have never heard anything about it! Where is it? What namespace?

avatar image fafase · Nov 24, 2014 at 06:43 AM 0
Share

it is from the asset store but this i unrelated to your problem here. itween is for interpolating and you are asking about coroutine.

avatar image Victor Fabiano · Nov 24, 2014 at 09:55 PM 0
Share

It kind of helped me... StopCoroutine("Fade"); didn't work for me... But I tried StopAllCoroutines(); and it did the work! Is there any problem using StopAllCoroutines ins$$anonymous$$d of StopCoroutine ? Thanks anyway!

avatar image Kiwasi · Nov 24, 2014 at 09:59 PM 0
Share

The risk of using StopAllCoroutines is you will stop coroutines you want to keep running.

StopCoroutine will only work if you use the string form of StartCoroutine. I believe this is changing in one of the upco$$anonymous$$g releases.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using coroutine to change current GUI 1 Answer

How do I return a reference to an object downloaded via a function? 0 Answers

yield behaves oddly with anonymous functions 1 Answer

StartCoroutine() and yield return www does not work with Unity Web Player build 0 Answers

LoadLevel*Aynsc() and Coroutine 3 Answers


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