Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by iAmAwsum · Jan 08, 2017 at 04:13 PM · c#unity 5uicoroutineunityui

Stop previous coroutine and start new one from beginning

I'm making a text for ammo to show up every time I shoot. Every time I shoot void update_ammo_text() gets called.

The whole code looks like this.

 public static void update_ammo_text(int ammo) {
         Coroutine tempCoroutine = null;
         if(tempCoroutine != null)
             instance.StopCoroutine(tempCoroutine);
         tempCoroutine = instance.StartCoroutine(show_ammo_text(ammo));
     }
 
     static IEnumerator show_ammo_text(int ammo) {
         ammo_text.enabled = true;
         Color newColor = new Color(Random.value, Random.value, Random.value, 1f);
         ammo_text.color = newColor;
         ammo_text.text = "AMMO LEFT ~ " + ammo;
 
 
         yield return new WaitForSeconds(1f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.9f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.8f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.7f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.6f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.5f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.4f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.3f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.2f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0.1f);
         yield return new WaitForSeconds(0.05f);
         ammo_text.color = new Color(newColor.a, newColor.g, newColor.b, 0f);
 
 
         ammo_text.enabled = false;
     }

If I shoot and wait for the coroutine to finish, and shoot again it works as it should. But if I should with an automatic rifle (does not let coroutine finish first) the a value in the color does not reset. It continues where it left off.

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
Best Answer

Answer by hoekkii · Jan 08, 2017 at 04:43 PM

I guess something like this, I would not recommend this structure. Maybe use a tween library or create your own, for easier aborting and stuff. Note I did not test this, but it should work.

     static bool StopShowAmmoText;
     static IEnumerator show_ammo_text(int ammo)
     {
         // Stop the other called enumerators and wait one frame
         StopShowAmmoText = true;
         yield return null;
         StopShowAmmoText = false;
 
         // Settings
         const float WaitTime = 1.0f;
         const float ShowTime = 0.5f;
         const int Increments = 10;
         Color newColor = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value, 1f);
 
         // Apply the text and color
         ammo_text.enabled = true;
         ammo_text.color = newColor;
         ammo_text.text = "AMMO LEFT ~ " + ammo;
 
         // Keep doing this wile the time this function is running is lower than (WaitTime + ShowTime)
         float time = 0.0f;
         while (time < (WaitTime + ShowTime))
         {
             // Raise the time
             time += Time.deltaTime;
 
             // Wait for WaitTime amount of second
             if (time < WaitTime)
             {
                 // Keep checking if we need to stop
                 if (StopShowAmmoText) { yield break; }
                 yield return null;
                 continue;
             }
 
             // Calculate the new color
             float t = 1.0f - Mathf.InverseLerp(WaitTime, (WaitTime + ShowTime), time);
             newColor.a = Mathf.Round(t * Increments) / Increments; // This one uses increments
             // newColor.a = t; // Use this one for fully smooth
             ammo_text.color = newColor;
 
             // Check if we need to stop
             if (StopShowAmmoText) { yield break; }
             yield return null;
         }
 
         // We finished so disable the text
         ammo_text.enabled = false;
     }

Comment
Add comment · Show 2 · 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 iAmAwsum · Jan 08, 2017 at 05:01 PM 0
Share

Thank you! This is just what I needed. I've just copy-pasted this into my game to test it and it works. Only problem is the text waits, then dissapears, appears and then dissapears again. I'll take the time to understand what you wrote. I rewarded you. Have a good day!

avatar image hoekkii iAmAwsum · Jan 08, 2017 at 05:51 PM 1
Share

No problem. I just added some comments, maybe it helps. Also probably solved the thing you described?? If you got any further questions or questions about the code, just ask. Thank you! You too have an awesome day!

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

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

Calling coroutine in a method issue. 1 Answer

Visible popup damage notifier 0 Answers

How to resize image to what's inside? 0 Answers

[SOLVED]My UI text don't update from script 3 Answers

Trying to move buttons via Coroutine and transform.Translate 0 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