- Home /
Update not being called every frame
Hi everyone, I have a script here that is supposed to regenerate my shields if the curShield is less than maxShield. It works but it only calls the AdjustCurrentShield function when I am moving, it wont work if I am just standing still. Can someone take a look and tell me where I went wrong?
static var maxHealth : int = 100;
static var curHealth : int = 100;
static var curShield : int = 100;
static var maxShield : int = 100;
static var adjs : int;
static var adj : int;
var shieldText : Texture2D;
var healthText : Texture2D;
var healthBarLength : float;
var shieldBarLength : float;
var shieldRegen : float = 5;
// Use this for initialization
function Start() {
healthBarLength = 250;
shieldBarLength = 250;
}
// Update is called once per frame
function Update() {
AdjustCurrentShield();
AdjustCurrentHealth();
}
function OnGUI() {
GUI.DrawTexture(new Rect(10, 10, shieldBarLength, 24), shieldText);
GUI.DrawTexture(new Rect(10, 36, healthBarLength, 24), healthText);
}
function AdjustCurrentHealth() {
curHealth += adj;
if(curHealth <= 0)
Destroy(gameObject);
if(curHealth > maxHealth)
curHealth = maxHealth;
healthBarLength = 400 * curHealth / maxHealth;
}
function AdjustCurrentShield() {
curShield += shieldRegen * Time.deltaTime;
if(curShield < maxShield)
curShield += shieldRegen * Time.deltaTime;
if(curShield > maxShield)
curShield = maxShield;
if(maxShield < 1)
maxShield = 1;
shieldBarLength = 400 * curShield / maxShield;
}
Answer by eem · Aug 28, 2011 at 09:46 AM
Thats not possible. You AdjustCurrentShield function is getting called every frame.
A good technique for debugging is to lay Debug.Log calls throughout your script.
So at the begenning of your AdjustCurrentShield function put:
Debug.Log("Adjust Current Shield Called!");
this will print out a message so you can see it being called every frame.
Now if a variable or something is not reacting as you expect, add a Debug.Log call to watch that variable.
for example... Debug.Log(curShield);
then see if the variable is outputting what you expect
Ok so I added the debug statements and they ARE being called but the shield regen still isn't working. So do you see anything wrong the code that might be causing it to not work?
Answer by hodev · Jan 26, 2013 at 04:12 AM
OK this is a pretty old post, but I landed on it and there's still no answer, so I thought I'd answer..
Regarding this line: curShield += shieldRegen * Time.deltaTime
curShield is an int, and you're adding a float that is being multiplied by Time.deltaTime. It is possible that the result of 'shieldRegen * Time.deltaTime' is less than 1, and so when added to an int, adds zero.
Maybe while your character is moving, it drops the framerate enough (increase Time.deltaTime) to bump the result over 1, thereby suddenly working.
Changing curShield to a float should fix the problem. Then you might want to round shieldBarLength before assigning it.
Your answer
Follow this Question
Related Questions
How to destroy in an amount of time 1 Answer
Minute Timer Issue 1 Answer
Time based scoring 2 Answers
Decrease Health every 5 mins or Time script? 2 Answers
Accumulating deltaTime oddity 1 Answer