0
\$\begingroup\$

I'm trying to regenerate stamina at a rate of 1 per second using Time.deltaTime, however this doesn't do anything (My 'current stamina' doesn't change at all). I've done some looking around and I have tried Time.fixedDeltaTime. Also tried Time.time, but that just regenerated all my stamina in under a second.

Here is my code:

var max_stamina : int = 100;
var cur_stamina : int;
var staminaRegenRate : int = 1;

function Update(){
    //regen
    if(cur_stamina < max_stamina){
        cur_stamina += staminaRegenRate * Time.deltaTime;
        Debug.Log("Second has passed");
    }
}

What am I doing wrong?

\$\endgroup\$
  • \$\begingroup\$ At which rate does FixedUpdate() is called? Every second? \$\endgroup\$ – Alexandre Desbiens Jul 21 '14 at 11:54
  • \$\begingroup\$ @AlphSpirit wrote that wrong, was in function Update() \$\endgroup\$ – eren_tetik Jul 21 '14 at 11:58
  • \$\begingroup\$ Debug.Log("Second has passed"); is called each time Update is called, that is why it seems weird. \$\endgroup\$ – Alexandre Desbiens Jul 21 '14 at 12:00
  • \$\begingroup\$ @AlphSpirit I put that in to see if it would log once a second. It was logging nearly 100 times a second. That line of code isn't really important to the rest of the code \$\endgroup\$ – eren_tetik Jul 21 '14 at 12:03
4
\$\begingroup\$

I guess since you are declaring the variables as integer, when you do cur_stamina += staminaRegenRate * Time.deltaTime; it is rounded to the nearest integer, effectively not changing its value.

Declare them as floats or doubles (and be more careful with data types!).

\$\endgroup\$
  • \$\begingroup\$ This. I am not too familiar with Javascript (and thought data types didn't even exist like in PHP) but this is the most likely the source of the problem. \$\endgroup\$ – Alexandre Desbiens Jul 21 '14 at 12:04
  • \$\begingroup\$ Strict types don't exist in JavaScript. It looks like you're using TypeScript, a language which compiles to JavaScript. \$\endgroup\$ – Mike Cluck Jul 21 '14 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.