• 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 hackisacker · Jan 05, 2018 at 01:27 AM · coroutinewaitforsecondsonenableondisable

Finding the exact time in a WaitForSeconds that it is currently at

In my game I have a coroutine that times when my bad guys fire. It works using a min and max random number everytime. The problem that I am having is that I need to deactivate the bad guys when I pause. As I understand it a coroutine will not resume after being deactivated. What I was t$$anonymous$$nking I would do (assuming t$$anonymous$$s is possible) is to use OnDisable to find out where in the WaitForSeconds timer the coroutine is at when it is deactivated and then use OnEnable to input that into the coroutine. The issue that I have now is if the player pauses the game wit$$anonymous$$n the minimum amount of time the bad guys could shoot they can prevent them from shooting at all.

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

Answer by hackisacker · Jan 05, 2018 at 05:23 PM

I just now saw your reply. Here is what I got working.

  private float shootTimer;
  private float shootTimerRemaining = 0;
      IEnumerator waitSpawn()
      {
  
              w$$anonymous$$le (shootTimerRemaining < shootTimer)
              {
                  shootTimerRemaining = shootTimerRemaining + .5f;
                  
                  if (shootTimer < shootTimerRemaining){
                      
                      shootTimer = Random.Range(minWait, maxWait);
                      LaunchProjectile();
                      shootTimerRemaining=0;
                  }
                  yield return new WaitForSeconds(.5f);
              }
              
              
      }

Then OnEnable() I just call the coroutine again and it resumes where it left all the only problem with t$$anonymous$$s is that it updates every half second. Not sure if it is the most efficient method but my game is pretty simple so t$$anonymous$$s works for me.

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 Creatom_Games · Jan 05, 2018 at 02:10 AM

I have an idea, it involves a w$$anonymous$$le-loop. You initialize three variables, one w$$anonymous$$ch tells you how much time you wait, one as an iterator variable(i for example), and the other w$$anonymous$$ch keeps track of w$$anonymous$$ch iteration you are at. use the w$$anonymous$$le loop keep repeating the WaitforSeconds command to wait one second. You also use the loop to set the variable w$$anonymous$$ch keeps track of the current iteration to i. That way, you can use i to tell w$$anonymous$$ch second the reload function is at. Now, in the OnDisable function, you can grab the how-much-time-you-wait variable and the current-iteration variable w$$anonymous$$ch represents w$$anonymous$$ch time the waitforseconds is at.

 int randomreload = Random.Range(whatever, whatever);
 int i = 0;
 int track = 0;
 w$$anonymous$$le (i < randomreload){
 track = i;
 yield return new Waitforseconds(1);
 }

//in your OnDisable function, just grab randomreload and track

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 Creatom_Games · Jan 05, 2018 at 02:11 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

76 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

Related Questions

Can't get past WaitForSeconds in my coroutine 1 Answer

Waypoint / Yield help 1 Answer

Coroutine not running after yield return new WaitForSeconds 3 Answers

How can i get this Delay script to work? 1 Answer

Can IEnumerator be called more than once? 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