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 Sky0812 · Mar 05, 2019 at 09:30 AM · coroutinetimer countdownpowerup

Coroutine starts but doesn't run?

I am trying to implement a powerup that ends after a certain time and if another powerup is grabbed then time adds to the timer before de-activation.

I put my coroutine for the powerup time onto a PowerupManager script and made it public and static, and start it when the pickup is grabbed, like so:

          if (PowerupManager.powerupActive == false)
         {
             PowerupManager.powerupActive = true;
             PowerupManager.timer = powerupTime;
             StartCoroutine(PowerupManager.PowerupTimer());
         }
         else
         {
             PowerupManager.timer += powerupTime;
         }



Pretty straightforward. Then, the PowerupManager's coroutine should activate and do its thing:

public class PowerupManager : MonoBehaviour {

 public static bool powerupActive = false;
 public static float timer = 0f;

 //Waits amount of time and then disables the powerup effects
 public static IEnumerator PowerupTimer()
 {
     //At start of coroutine, set seconds elapsed to zero
     float seconds = 0f;

     //While seconds elapsed is less than the time to run, keep running this timer
     //Check the condition every second in case the timeToKeepActive changes
     while (seconds < timer)
     {
         Debug.Log((timer - seconds) + "seconds left to run");
         yield return new WaitForSeconds(1f);
         seconds++;
     }

    //[Code to deactivate powerup goes here]

     powerupActive = false;
 }

}


My pickup's powerupTime is set to 8f, so I should get the Debug.Log((timer - seconds) + "seconds left to run"); to print the amount of time left to run, counting down from 8 to zero. However it only prints "8 seconds left to run", and doesn't countdown any further. Not sure what's going on here so any help is appreciated!

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 Hellium · Mar 05, 2019 at 09:56 AM 0
Share

Are you sure the object running the coroutine isn't disabled before the latter ends?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Mar 05, 2019 at 10:13 AM

This line:

 StartCoroutine(PowerupManager.PowerupTimer());

is equivalent to

 this.StartCoroutine(PowerupManager.PowerupTimer());

That means you run the coroutine on the MonoBehaviour instance you're currently on. Since i guess this is a powerup object which you probably destroy or deactivate the coroutine will be terminated along with the object. Since you already have a PowerupManager you probably should start the coroutine on that instance.


Instead of using so many static variables, it's better to make your PowerupManager a singleton like this:

 public class PowerupManager : MonoBehaviour
 {
     public static PowerupManager instance;
     public bool powerupActive = false;
     public float timer = 0f;
     void Awake()
     {
         instance = this;
     }
     private IEnumerator PowerupTimer(float aTime)
     {
         powerupActive = true;
         float seconds = 0f;
         timer = aTime;
         while (seconds < timer)
         {
             yield return new WaitForSeconds(1f);
             seconds++;
         }
         powerupActive = false;
     }
     
     public void PowerupPickup(float aTime)
     {
         if (powerupActive == false)
         {
             StartCoroutine(PowerupTimer(aTime));
         }
         else
         {
             timer += aTime;
         }
     }
 }

Inside your Powerup object you just would do:

 PowerupManager.instance.PowerupPickup(powerupTime);

Of course your PowerupManager need to be attached to an object in the scene.

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 Sky0812 · Mar 05, 2019 at 05:59 PM 0
Share

You guys nailed it, my powerup deletes itself after starting the timer, I wasn't aware that the coroutine runs on whichever script calls it. 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

110 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

Related Questions

Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object 2 Answers

yield return WaitForSeconds not working 2 Answers

Timer with Coroutine 3 Answers

Instantiate an array of gameobjects with a time delay between each 1 Answer

Increment variable over time 4 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