Search Unity

Why my score it going down really fast when using Time.deltaTime?

Discussion in 'Scripting' started by JediJaimie, Mar 29, 2015.

  1. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    Why my score it going down really fast when using Time.deltaTime? I'm trying to make a score system when every second you loss 1 point, but when I try to do this it make it go everything fast. I tried playing around with the TimeScale, fixed update, fixedtime and stuff but nothing to seem to work. Is this something have to do with the UI? What am I doing wrong? Here's my code

    Code (CSharp):
    1. public float timer = 0;
    2.     private string currentTime;
    3.     Text text;
    4.  
    5.     void Start()
    6.     {
    7.         text = GetComponent<Text> ();
    8.         score = 50000;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         timer += Time.deltaTime;
    14.         currentTime = string.Format ("{0:0}", timer);
    15.  
    16.         score -= (int) timer;
    17.  
    18.         if (score < 0)
    19.             score = 0;
    20.  
    21.         text.text = "" + score;
    22.     }
    You can link me to other questions are like this if there are.
     
    Last edited: Mar 29, 2015
  2. 1UnityUser12

    1UnityUser12

    Joined:
    Jul 22, 2013
    Posts:
    4
    It seems like the timer goes down and the score goes up
     
  3. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    I know i was playing around with this so ill change it. And for some reason its doing the same thing.
     
  4. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    The mistake you've made is that you increase the timer then take it away from the current score. So it won't remove 1 a second it will remove the time elapsed every frame.

    You can do something like this to get what you want. Alternatively you can change the line "score -=(int) timer;" to "score -= Time.deltaTime;"

    Code (CSharp):
    1.  
    2. public float timer = 0;  
    3. private string currentTime;  
    4. Text text;
    5. int baseScore;
    6. int curScore;
    7.  
    8. void Start()  {      
    9. text = GetComponent<Text> ();      
    10. baseScore = 50000;
    11. }  
    12.  
    13. void Update()   {      
    14. timer += Time.deltaTime;      
    15. currentTime = string.Format ("{0:0}", timer);      
    16. curScore = Mathf.RoundToInt(baseScore - (int) timer);
    17. if (curScore < 0)
    18. curScore = 0;
    19. text.text = "" + curScore;  
    20. }
    21.  
     
    Last edited: Mar 30, 2015
    JediJaimie likes this.
  5. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    Thank you LordConstant, it works and now i understand! :)
     
  6. Jananton

    Jananton

    Joined:
    Oct 8, 2014
    Posts:
    22
    Hmm, I'm more a C++ then a C# person, but wouldn't the following be much more simple.

    Code (CSharp):
    1.     private float lastTime;
    2.  
    3.     void Start() {
    4.         lastTime = Time.time;
    5.     }
    6.  
    7.     void Update () {
    8.         if (Time.time - lastTime > 1) {
    9.             score--;
    10.             lastTime = Time.time;
    11.         }
    12.     }
    Greets,

    Jan
     
    JediJaimie likes this.
  7. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    At that point you are just calculating the delta time though. Which can be accessed with Time.deltaTime.

    But you are right that way can be done. I just decided to adapt the code to fit with the current method.
     
    JediJaimie likes this.