Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 wenxuan2720 · Apr 15, 2019 at 07:37 AM · coroutinecoroutinesyield waitforseconds

coroutine not running after yield new WaitForSeconds

Hi everyone,

so I am creating this mobile. Basically its like an endless runner that goes up. So the problem is with this script that I have for this powerup. This powerup basically just increases the speed of the player and all for a short period of time. This is why I had to use a coroutine and yield new waitforSeconds. The problem here is that sometimes it works perfectly fine and sometimes everything that happens after either the first waitforseconds or the second one just does not run. This code used to work fine (yesterday) until today it suddenly just stopped working.

     IEnumerator PickupSpeed(Collider2D player)
     {
 
         turboParticleEffect = Instantiate(pickupEffect, player.transform.position, transform.rotation);
         turboParticleEffect.transform.parent = player.transform;
 
         GetComponent<SpriteRenderer>().enabled = false;
         GetComponent<Collider2D>().enabled = false;
 
         player.GetComponent<CircleCollider2D>().enabled = false;
         player.GetComponentInChildren<PolygonCollider2D>().enabled = true;
 
         OldMoveSpeed = GM.moveSpeed;
 
         GM.Turbo = true;
 
        UpdateplayerSR(1f, playerBlankColor, "yellow", null);
 
         yield return new WaitForSeconds(duration);
 
 
         GM.Turbo = false;
 
         Destroy(turboParticleEffect);
 
         yield return new WaitForSeconds(2f);
 
         //UpdateplayerSR(1f, Color.blue, "white", null);
         Debug.Log("almost finished turbo");
 
         playerSR.color = playerBlankColor;
 
         Debug.Log("finished turbo");
         
         player.GetComponent<CircleCollider2D>().enabled = true;
 
         Destroy(gameObject);
 
     }
 
 
 public void UpdateplayerSR(float transitionTime, Color fadeColor, string c, Action func)
     {
        // playerSR.color = fadeColor;
         StartCoroutine(UpdateplayerSRcolor(transitionTime, c, func));
     }
 
     private IEnumerator UpdateplayerSRcolor(float transitionTime, string color, Action func)
     {
         float t = 0.0f;
 
         for (t = 0.0f; t <= 1; t += Time.deltaTime / transitionTime)
         {
             if (color == "red")
             {
 
                 //playerSR.color = new Color(t, SR.color.g, SR.color.b);
 
                 playerSR.color = Color.Lerp(Color.white, Color.red, t);
 
             }
 
             if (color == "green")
             {
                 //playerSR.color = new Color(0, t, 0);
                 playerSR.color = Color.Lerp(Color.white, Color.green, t);
             }
 
             if (color == "blue")
             {
                 //playerSR.color = new Color(0, 0, t);
                 playerSR.color = Color.Lerp(Color.white, Color.blue, t);
             }
 
             if(color == "white")
             {
                 //playerSR.color = new Color(t, t, t);
                 playerSR.color = Color.Lerp(Color.red, Color.blue, t);
             }
 
             if (color == "yellow")
             {
                 //playerSR.color = new Color(t, t, 0);
                 playerSR.color = Color.Lerp(Color.white, Color.yellow, t);
             }
 
             yield return null;
 
 
         }
 
         //SR.color.a = 1;
 
         func?.Invoke();
     }
 }
 


so I am new with unity and this is my first project. I know, my code is not neat at all and if you have any suggestions on that I would to happy to hear about them too. Would the fact that a coroutine running inside a corountine make it lag?

Comment
Add comment · Show 1
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 wenxuan2720 · Apr 15, 2019 at 05:16 AM 0
Share

oh and I have looked at other answers and those that I have seen don't apply to what I am doing and I have tried debugging with debug.log and it seems it was not running if it was after yield return new WaitForSeconds is not running.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by xxmariofer · Apr 15, 2019 at 07:56 AM

thats probably because your object that has the script with the monobehaviour gets destroyed.

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

111 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

Related Questions

NullReferenceException in StartCoroutine 1 Answer

How to stop a coroutine started in Script A from within Script B 1 Answer

How to change this using a coroutine 1 Answer

Loop Coroutine For A Demo Mode 1 Answer

yield return new WaitForSeconds not working 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