Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Demigoth · May 07, 2014 at 01:15 AM · coroutinecoroutines

coroutine help

Hello, so I got a coroutine with a WaitForSeconds() statement, but I want the coroutine to have 2 ways to continue for the player, to wait for x seconds before the next enemy spawns or hurry the time for the next spawn with a button press. Any idea on how this can be done? I just need a general idea, I am not asking for any code, thank you all in advance!!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 07, 2014 at 01:27 AM

Yes.

Write the code that goes on as a separate routine that is called either from your co-routine's continuation or directly by a user press. Have it set a boolean that says its been called so, if the user calls it, and then the co-routine completes, it will just ignore the second call.

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
avatar image
0

Answer by robertbu · May 07, 2014 at 01:28 AM

I know you did not want any code, but sometimes an example is faster and better than a long description. There are a number of different approaches. Here is one. Put this on an empty game object and run. Space will speed the current cycle.

 #pragma strict
 
 var abort = false;
 
 function Start() {
     DoTheAction(5.0);
 }
 
 function DoTheAction(time : float) {
     while (true) {
         var timer = 0.0;
 
         while (timer < time && !abort) {
             
             Debug.Log(timer);
             timer += Time.deltaTime;
             yield;
         }
         Debug.Log("#######Instantiate#######");
         abort = false;
         // Instantiate code goes here;
     }
 }
 
 function Update() {
     abort = Input.GetKeyDown(KeyCode.Space);
 }

Note that by using just 'yield' and a timer, instead of 'yield WaitForSeconds()', we can check to see if the user hit a key each frame.

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

21 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

Related Questions

Nested Coroutines: last coroutine quits early 1 Answer

I need to Stop and Restart a Co-Routine at anytime 1 Answer

coroutine not running after yield new WaitForSeconds 1 Answer

How to properly implement a constant queue of actions in the background,How to properly implement a continuous background queue of actions? 1 Answer

Why doesn't a coroutine start executing from beginning when I start it again? 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