Script does not count up?

Hello all,

I wanted to make a runscript which only makes you able to run for X amount of time. Problem I am having is that it doesn’t regenerate the runbar. The Runbar += amount doesn’t work? even though if I change it to -= it works. It is very odd and not sure why this happens.

Hopefully someone can help me.

MyRunScript :
#pragma strict

var RunBar : int = 100;
var walkSpeed : float = 7;
var runSpeed : float = 20;
var RemoveRatePS : float = 10;
var RegenRatePS : float = 5;

private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;

function Start () {
	chMotor = GetComponent(CharacterMotor);
	tr = transform;
}

function Update () {
    var speed = walkSpeed;
    
    if (RunBar > 0 && Input.GetKey(KeyCode.LeftShift)||Input.GetKey(KeyCode.RightShift)) {
    	speed = runSpeed;
    	
    	RunBar -= (RemoveRatePS/20);
    } else {
    Debug.Log("Regenerating!");
      RunBar += (RegenRatePS/20);
    }
    
    chMotor.movement.maxForwardSpeed = speed;
}

I added a debug log to be sure if it got at that point which it does.

You are trying to add a fractional value to an int. That is 5/20 = .25. This fractional value will be truncated when added to an int. You can solve your problem by making RunBar a float.