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 Leon123442 · Jul 31, 2018 at 04:02 PM · scripting problemyieldwait

How can I remove 1every second from integer A while bool B is true?

I am trying to script a generator that uses up one fuel when it is on every second.

The script I tried to use:

 public IEnumerator usingfuel()
 {
     while (generatorOn == false)
     {
         yield return null;
     }

     yield WaitForSeconds(10);
     fuelcount -= 1;
 }

Why does it not work and how can I improve it?

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

Answer by Hellium · Jul 31, 2018 at 04:41 PM

Try this (not tested)

 public IEnumerator usingfuel( float interval ) 
 {
     float timer = interval ;
     while( true )
     {
         if( generatorOn )
         {
             timer -= Time.deltaTime ;
             if( timer < 0 )
             {
                 fuelcount -= 1 ;
                 timer = interval
             }
         }
         yield return null
     }
 }

You will have to run this coroutine once, from the Start function for instance.

 void Start()
 {
      StartCoroutine( usingfuel( 10 ) ) ;
 }
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
1

Answer by MacDx · Jul 31, 2018 at 04:40 PM

There are various problems with this code.


First, it won't even compile because yield WaitForSeconds(10); is syntactically wrong. This is the correct syntax

 yield return new WaitForSeconds(10);

Second, you say you want to decrease fuel every second but you are passing a 10 to WaitForSeconds instead of 1?

Third, this coroutine will only decrease fuel once and then stop, I don't know if you are starting it multiple times but that would be bad since it defeats the point of having a coroutine in the first place (which is to have something run continuously and in "parallel" to Update).


This is how I'd do it:

 public IEnumerator UsingFuel()
 {
     while(!gameOver)
     {
         if(generatorOn)
             fuelcount --;
         yield return new WaitForSeconds(1f);
     }
 }

Notice that I added an extra bool. This way you just start this coroutine at Start, and if you change generatorOn to true or false it will just work. And if you want to stop it then you just set gameOver to true.


Hope this helps.

Comment
Add comment · Show 2 · 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 Hellium · Jul 31, 2018 at 04:45 PM 0
Share

A very skilled player could prevent the fuel from decreasing if he / she releases the button responsible for turning on generatorOn only when the coroutine "ticks" at t = 0, t = 1, t = 2, ....

avatar image MacDx Hellium · Jul 31, 2018 at 04:47 PM 0
Share

Yeah, if generatorOn is tied to to input then yes, but we don't know that.

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

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

NEED HELP PLEASE: When i kill a one of the three limited "mobSpawned" the "while (true)" don't work and don't spawn a new object. Anyone have a solution for this. Thanks. 1 Answer

How to use IEnumerator? Pls help! 1 Answer

reload script assemblies is VERY Slow. 1 Answer

Using rb.velocity causes low gravity. 2 Answers

Scripting Movement Fails for Instantiated Prefab 2 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