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 13, 2013 at 10:38 PM · floatintegernumberstamina

Why aren't these numbers going down by 1.

Hey. So I got this script to show stamina in my game. It's in C# and I'm not good at it. If found it at a forum and added some bits to it to fit me. One of which is displaying the stamina, at the bottom in GUI. The problem I have is that when I start running the number goes down by decimals and not whole numbers so the whole thing looks weird on screen. How do I make it so it simply goes down 1 by 1 and not in decimals.

 using UnityEngine;
 using System.Collections;
 
 public class StaminaRun : MonoBehaviour
 {
     private float barLength = 0.0f;
     
     // Max running time
     public float RunningTime = 10f;
 
     // How fast do we regenerate available running time?
     public float RestRate = 1f;
 
     // Player must be able to run for at least X seconds
     public float RunThreshold = 2f;
 
     private vp_FPPlayerEventHandler Player;
     public float availableRunTime;
 
     protected virtual void Awake()
     {
 
         // cache the player event handler
         Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler;
         availableRunTime = RunningTime;
 
     }
 
     /// <summary>
     /// registers this component with the event handler (if any)
     /// </summary>
     protected virtual void OnEnable()
     {
 
         // allow this monobehaviour to talk to the player event handler
         if (Player != null)
             Player.Register(this);
 
     }
 
     /// <summary>
     /// unregisters this component from the event handler (if any)
     /// </summary>
     protected virtual void OnDisable()
     {
 
         // unregister this monobehaviour from the player event handler
         if (Player != null)
             Player.Unregister(this);
 
     }
 
     public void Update()
     {
         if (!Player.Run.Active)
         {
             // If we're not running, regenerate our stamina
             availableRunTime = Mathf.Min(availableRunTime + Time.deltaTime * RestRate, RunningTime);
             return;
         }
 
         // Decrease our running time available and see if we have to stop
         availableRunTime -= Time.deltaTime;
         if (availableRunTime <= 0)
         {
             Player.Run.TryStop();
             availableRunTime = 0;
         }
     }
 
     protected virtual bool CanStart_Run()
     {
         // Only enable running if we're above the running threshold
         if (availableRunTime >= RunThreshold)
             return true;
 
         return false;
     }
     
     void  OnGUI ()
     {
 
     GUI.Box(new Rect(5, 130, 55, 23), "Stamina");
         
     GUI.Box(new Rect(62, 130, barLength, 23), availableRunTime + "/" + 10);
     
     }
     
     void  Start ()
     {
         barLength = Screen.width / 7;
     }
 
 }
 
 
Comment
Add comment · Show 3
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 meat5000 ♦ · Oct 13, 2013 at 10:42 PM 1
Share

I'll give you some hints...

This line

  availableRunTime -= Time.deltaTime;

each time its run decreases the value in availableRunTime by the value Time.deltaTime

Time.deltaTime is a FLOAT (decimal)

Google search :

Unity Time.deltaTime, int, float, parse.

The last one can help you quickly but not efficiently. I could spell it out but you'll learn more this way...

avatar image Trollvahkiin · Oct 13, 2013 at 10:51 PM 0
Share

I searched it up and I got availableRunTime -= Time.deltaTime / 1; I already tried that but no luck.

avatar image meat5000 ♦ · Oct 13, 2013 at 11:36 PM 0
Share

1/1 = 1

200/1 = 200

  1. / 1 = 0.1

2000258.267 / 1 = 2000258.267

See where I'm going with this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DylanW · Oct 13, 2013 at 10:53 PM

This is because Time.deltaTime is a float. Try using Mathf.FloorToInt() to calculate it as an int, which is a whole number.

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 Trollvahkiin · Oct 13, 2013 at 11:05 PM 0
Share

Thanks, I got it half working. currentSta$$anonymous$$a = $$anonymous$$athf.FloorToInt(availableRunTime); I put that into the update and it works when sta$$anonymous$$a goes down but when it goes up it doesn't do anything. 'currentSta$$anonymous$$a' is a int var.

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

17 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

Related Questions

Golf swinging gauge - Techanically any type of recepricating gauge. 0 Answers

int myX = (int)transform.position.x; 1 Answer

Rounded particle motion 0 Answers

Floating Point and ToString issue 2 Answers

GUI.HorizontalSlider precision 3 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