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\$
4
  • \$\begingroup\$ At which rate does FixedUpdate() is called? Every second? \$\endgroup\$ Jul 21, 2014 at 11:54
  • \$\begingroup\$ @AlphSpirit wrote that wrong, was in function Update() \$\endgroup\$
    – eren_tetik
    Jul 21, 2014 at 11:58
  • \$\begingroup\$ Debug.Log("Second has passed"); is called each time Update is called, that is why it seems weird. \$\endgroup\$ Jul 21, 2014 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, 2014 at 12:03

1 Answer 1

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\$
2
  • \$\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\$ Jul 21, 2014 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, 2014 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .