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 siddharth3322 · Feb 20, 2016 at 03:07 PM · coroutinestartcoroutinecoroutine errorsunity4.6stopcoroutine

After stop coroutine it resumes its task when I call start coroutine

When player gets life I want to start my timer coroutine and when player loose life I want to stop timer coroutine. But when player gets an extra life at that time, I want to start again my timer coroutine.

For this task I have written following code. public void StartPowerUpEnumrator () { Debug.Log ("start power up...."); StartCoroutine (powerUpEnumerator); }

     public void StopPowerUpEnumrator ()
     {
         Debug.Log ("stop power up....");
         StopCoroutine (powerUpEnumerator);
     }
 
     IEnumerator GeneratePowerUp ()
     {
         while (true) {
             Debug.Log("wait...");
             yield return new WaitForSeconds (60f);
 
             float horzPos = Random.Range (-5f, 5f);
             float vertPos = Random.Range (-3f, 3f);
 
             Debug.Log("Instantiate power up..."); 
             int selectedPowerUp = Random.Range (0, powerUpsPref.Length);
             GameObject powerUpObj = Instantiate (powerUpsPref [selectedPowerUp], new Vector3 (horzPos, vertPos, 0f), Quaternion.identity) as GameObject;
             powerUpObj.transform.SetParent (transform);
         }
     }

But when a player gets an extra life and start timer coroutine, it will not wait for 60 seconds to generated power up, it directly call next statements after wait for seconds. I want when I start my timer coroutine it again need to wait for 60 seconds then execute next lines.

My log gives you more idea regarding problem.

alt text

Please give me some solution for this.

spaceshooterlog.png (17.5 kB)
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
0

Answer by C-Gabriel · Feb 20, 2016 at 07:51 PM

Coroutine are quite unfriendly in my opinion. You can use my SmartTimersManager package to solve this. Simply add TimersManager.SetTimer(this, 60f, GeneratePowerUp); whenever you want to set/reset the delayed function.

Comment
Add comment · Show 3 · 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 siddharth3322 · Feb 22, 2016 at 04:10 PM 0
Share

@C.Gabriel, can you please post new link? Because above link is not working.

avatar image C-Gabriel siddharth3322 · Feb 22, 2016 at 04:15 PM 0
Share

@siddharth3322 Edited. It should work now :)

avatar image siddharth3322 C-Gabriel · Feb 22, 2016 at 04:18 PM 0
Share

@C.Gabriel, you have given me awesome reply. Link for paid plugin. Thanks for your reply.

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

35 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

Related Questions

StopCoroutine not working. 0 Answers

Coroutine does not stop? 1 Answer

Coroutine Issue 1 Answer

IEnumerator instead of LateUpdate in unity 1 Answer

Get IEnumerator parameters with StopCoroutine 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