- Home /
"Script2" will not update variable in "Script1" (No error messages)
I have a controller script that controls a stamina bar...
using UnityEngine;
using System.Collections;
public class StaminaScript : MonoBehaviour {
public Texture2D staminaGuiTex;
public Texture2D staminaBlueTex;
public float currTime;
public static float staminaScale;
// Use this for initialization
void Awake () {
currTime = Time.time;
staminaScale = 221;
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift)){
if(currTime < Time.time){
if(staminaScale != 0){
staminaScale -= 0.25f;
}
currTime = Time.time;
}
}
}
public void OnGUI() {
GUI.DrawTexture(new Rect(Screen.width - 300, 94, 256, 64), staminaGuiTex, ScaleMode.StretchToFill, true, 0);
GUI.DrawTexture(new Rect(Screen.width - 283, 116, staminaScale, 18), staminaBlueTex, ScaleMode.StretchToFill, true, 0);
}
}
and I need this script to change add to it after the victim is attacked...
<if (((num <= this.biteDistance) && Input.GetButtonDown("Jump")) || (num2 <= this.biteDistance))
{
GUIKills.numberOfKills++;
StaminaScript.staminaScale = (StaminaScript.staminaScale + 221 * (25/221));
this.status = AIStatus.Attacked;
However, no matter if I use a static variable or not, no error messages appear, but the staminaScale variable doesn't update. Any suggestions?
EDIT: There is a third Script I try to change the variable and it works...
enter code herevoid Update ()
{
if(!died&&(GameController.health <= 0))
{
//StaminaScript staminaScript = GameObject.FindGameObjectWithTag("Stam").GetComponent<StaminaScript>();
StaminaScript.staminaScale = 221;
Die();
}
if(Input.GetButtonDown("Jump"))
{
gameObject.audio.PlayOneShot(audio.clip);
}
}
Are you sure that this line gets executed? Is GUI$$anonymous$$ills.numberOf$$anonymous$$ills increased. If you don't know try to add a Debug.Log right at the beginning of the if statement.
The GUI$$anonymous$$ills.numberOf$$anonymous$$ills does increase. I've also added a print() command to make sure it runs. The code runs good.
What happens if you add Debug.Log(Sta$$anonymous$$aScript.sta$$anonymous$$aScale);?
It doesn't change from before the line to after it. Same if I watch it in the inspector.
To better describe what is going on... When the player holds down shift, he moved faster and the sta$$anonymous$$a drains over time. When he kills an enemy, the sta$$anonymous$$a bar is suppose to refill 25%. If the player dies, the sta$$anonymous$$a bar is refilled completely.
Answer by Bunny83 · May 22, 2013 at 06:50 PM
That's simply because this expression evaluates to 0:
(25/221)
25 and 221 are integer values. When you divide an integer by an integer the result will be also an integer. 25 / 221 is 0.1131 which will be trucated to 0
Use float literals. Either:
25.0f / 221.0f
or just
25f / 221f
I prefer the first one because it's more obvious that it's a float.
ps: Never ever compare float values to a certain value unless you set it to this explicit value. This statement will probably never be false:
if(staminaScale != 0)
use a range instead:
if(staminaScale > 0)
Also if you don't want it to go below 0 you can clamp it wither with Mathf.Max or if you also have a maximum with Mathf.Clamp
staminaScale = Mathf.Max(staminaScale, 0.0f); // keep it above or equal to 0
staminaScale = Mathf.Clamp(staminaScale, 0.0f, 100.0f); // keep it between 0 and 100
Wow, I didn't think of that. At one point at had (float) before all of it, but I didn't think about the individual numbers.
I applied this change and everything is working great. Thanks so much!