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 Trollvahkiin · Oct 11, 2013 at 06:18 PM · countdownstats

How to decrease a number by 1 every second?

Hey, so I put together this code, it's fairly simple but I doesn't work. Instead of going down by 1 every second it goes to 0 with in 2 seconds. How do I fix it?

 #pragma strict
 
 var health : String = "Health: ";
 var hunger : String = "Hunger: ";
 var thirst : String = "Thirst: ";
 var sleepiness : String = "Sleepinees: ";
 
 var hungerLevel : int = 100;
 var thirstLevel : int = 100;
 var sleepinessLevel : int = 100;
 
 var speed : int = 1;
 
 
 function OnGUI()
 {
     GUI.Box (Rect (0,Screen.height - 25,100,25), health);
     GUI.Box (Rect (110,Screen.height - 25,100,25), hunger);
     GUI.Box (Rect (220,Screen.height - 25,100,25), thirst);
     GUI.Box (Rect (330,Screen.height - 25,110,25), sleepiness);
 }
 
 function Update()
 {
     hungerLevel -= Time.deltaTime * speed;
     thirstLevel -= Time.deltaTime * speed;
     sleepinessLevel -= Time.deltaTime * speed;
     
     hunger = "Hunger: " + hungerLevel;
     thirst = "Thirst: " + thirstLevel;
     sleepiness = "Sleepiness: " + sleepinessLevel;
 {
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 clunk47 · Oct 11, 2013 at 06:23 PM

Use a coroutine.

 #pragma strict
 
 var num : int = 100;
 
 function Awake()
 {
     CountDown();
 }
 
 function CountDown()
 {
     while(true)
     {
         
         num -= 1;
         yield WaitForSeconds(1);
     }
 }
 
 function OnGUI()
 {
     GUILayout.Label(num.ToString());
 }
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 ricotomo · Nov 23, 2014 at 05:36 PM 0
Share

could you explain this?

avatar image
0

Answer by Jignesh G. · Nov 23, 2014 at 05:54 PM

You can achieve it using a recursive co-routine which will decrease a no every second. See below Code Sample.

 int nos = 100;
 void Start ()
 {
     StartCoroutine ("descreseNos");
 }

 IEnumerator descreseNos()
 {
     yield return new WaitForSeconds(1F);
     nos -= 1;
     StartCoroutine ("descreseNos");
 }
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 Davinder_Singh · May 05, 2019 at 09:28 AM 0
Share

how to attach code here, i don't really know!!! I tried and it kinda messed up, i am sorry for out of context question but any help?

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

18 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

Related Questions

Programmatically change the Game Window resolution 2 Answers

I am trying to find ideal values or range of values of statistics as displayed under game tab of unity 0 Answers

i need help with my weapon stats 1 Answer

Coin Counting Problem 0 Answers

How to make a time based game? 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