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 puriti · May 06, 2012 at 03:06 PM · variableslowcountdowndecreaseslowly

Having trouble with slowly decreasing a variable

I've looked at some other peoples problems, but I just can't figure out what to fix exactly. My energy counter decreases too fast. I had a yield statement before that just made it pause for 2 seconds before decreasing really fast. I'm also having trouble stopping it once it hits 0. Unity decides to crash on me.

This is what I had before I had any issues with it other than it speeding fast:

 var energy = 100;
 
 
 function Start () {
 
 }
 
 function Update () {
 guiText.text= energy.ToString();
 Decrease();
 Debug.Log(energy);
 }
 
 function Decrease()
 {
 while (energy>=0){
 energy--;
 
 }
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 Lo0NuhtiK · May 06, 2012 at 03:15 PM

Here's one way :

 var energy : int = 100 ;
 var depleteBy : int = 1 ; //deplete energy by this much 
 var depleteSeconds : float = 1.5 ; //deplete energy by 'depleteBy' after this many seconds
  
 function Start(){
    //start up a function that kicks off in depleteSeconds, then repeats every depleteSeconds afterward
    InvokeRepeating("DepleteMe", depleteSeconds, depleteSeconds) ;
 }
  
 function DepleteMe(){
    energy -= depleteBy ;
    if(energy <= 0){
       energy = 0 ;
       CancelInvoke("DepleteMe") ; //end this repeater if out of energy 
    }
 }
Comment
Add comment · Show 5 · 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 puriti · May 06, 2012 at 03:22 PM 0
Share

I can't tell if it's even working because now my update function isn't showing anything with the GUI text or Debug Log X_x

avatar image Lo0NuhtiK · May 06, 2012 at 03:27 PM 0
Share

well, I didn't write your entire script to work by copy/paste... kinda figured you'd notice what was left out and know what you need to do to do whatever else you're wanting...

avatar image puriti · May 06, 2012 at 03:30 PM 0
Share

Oh I know. I added the update with the GUI text and stuff and it's just not showing up anymore in general. Lol. I'll figure it out.

avatar image Lo0NuhtiK · May 06, 2012 at 03:32 PM 0
Share

Just add this at the bottom for a quick run :


   function OnGUI(){
      GUILayout.Box("Energy = : " + energy.ToString() ) ;
   }
avatar image puriti · May 06, 2012 at 03:34 PM 1
Share

I got it to work :) Thank you!

$$anonymous$$y GUI Text on my scene disappeared for some reason so my script wasn't attached to anything, but I had it working lol. Thank you! :D

avatar image
0

Answer by Piflik · May 06, 2012 at 03:15 PM

Change your Decreas function to the following:

 function Decrease() {
     if(energy > 0)
         energy--;
 }   

Your problem was that you have a while loop in your function, so the first time it is called, it will decrease the energy to 0.

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 Lo0NuhtiK · May 06, 2012 at 03:22 PM 0
Share

It's still going to decrease it every update call that way though.

avatar image Piflik · May 06, 2012 at 03:28 PM 0
Share

But it won't decrease completely on the first update. And now yield WaitForSeconds() should work as expected.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Question. [Simple] & [JS] 1 Answer

Variable decreases multiple times 0 Answers

Using a variable for decreasing health? 1 Answer

Variable not decreasing during runtime (C#) 1 Answer

Adding a value to a variable every second 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