- Home /
GUI Variables doesn't update C#
Hi, I'm stuck with this problem and I can't resolve it.
I'm trying to make a healt-bar which diplays both health and lives. They are controlled by two simple integers. When I try to change these integers from other scripts, they actually change in the inspector, but they don't in the GUI. For example, if I have 3 lives, and I add a life from another script, I still see 3 in game, but if I pause the game and check in the inspector I see 4. Another weird thing is that if I stop playing the game, and start again, it shows 4 in the GUI! Same thing happens with energy bar, while I have other variables shown in the GUI which uptade normally.
There is the code of the GUI.
using UnityEngine;
using System.Collections;
public class HUD : MonoBehaviour {
public Texture2D cursore;
public Texture2D cube;
public Texture2D hammer;
public Texture2D plane;
public Texture2D magnete;
public Texture2D shoes;
public Texture2D life;
public Texture2D fullbar;
public Texture2D emptybar;
public int numc;
public int numh;
public int nump;
public string strc;
public string strh;
public string strp;
public string strlif;
public GUIStyle style;
public bool magnet;
public bool shovel;
public bool portalgun;
public bool shoe;
public Texture2D pogu;
public Texture2D pala;
public GameObject ply;
public CharacterMotor cmj;
public int nrg;
public int lifes;
void Start(){
Screen.showCursor = false;
style.fontSize = 20;
style.alignment = TextAnchor.UpperCenter;
cmj = ply.GetComponent<CharacterMotor>();
}
void Update(){
strc = numc.ToString();
strh = numh.ToString();
strp = nump.ToString();
strlif = lifes.ToString();
}
void OnGUI(){
GUI.DrawTexture (new Rect(5, 5, 100, 100), life);
GUI.DrawTexture (new Rect(100, 53, 500, 50), emptybar);
GUI.DrawTexture (new Rect(100, 53, nrg, 50), fullbar);
GUI.DrawTexture (new Rect((Screen.width/2)-24, (Screen.height/2)-24,48,48), cursore);
GUI.DrawTexture (new Rect(Screen.width - 75, 1, 71, 71), hammer);
GUI.DrawTexture (new Rect(Screen.width - 146, 1, 71, 71), cube);
GUI.DrawTexture (new Rect(Screen.width - 217, 1, 71, 71), plane);
GUI.Label (new Rect(Screen.width - 49, 5, 20, 40), strh, style);
GUI.Label (new Rect(Screen.width - 118, 5, 20, 40), strc, style);
GUI.Label (new Rect(Screen.width - 188, 5, 20, 40), strp, style);
GUI.Label (new Rect(43, 43, 20, 40), strlif, style);
if (magnet == true){
GUI.Label (new Rect(Screen.width - 298, 1, 71, 71), magnete);
}
if (shovel == true){
GUI.Label (new Rect(Screen.width - 362, 1, 71, 71), pala);
}
if (portalgun == true){
GUI.Label (new Rect(Screen.width - 447, 1, 71, 71), pogu);
}
if (shoe == true){
cmj.jumping.extraHeight = 4.1f;
GUI.Label (new Rect(Screen.width - 518, 1, 71, 71), shoes);
}
}
}
The variables I'm talking about are "nrg" and "lifes".
Hope someone can help me. I'm sorry if I wrote something wrong, but I'm Italian :D
here's a though why don't you try using Debug.Log on strlif? does it change along (which it should ) with lifes?
void Update(){
strc = numc.ToString();
strh = numh.ToString();
strp = nump.ToString();
strlif = lifes.ToString();
//be sure to have the "collapse" option checked in the console!!
Debug.Log(strlif);
}
I set 3 at the start and subtracted 1 in game. Debug still shows "3", but the inspector shows 2 correctly.
then this strlif = lifes.ToString()
isn't working... which is incredibly strange.... since ToString() is a default system function and has nothing to do with unity or its GUI.
this means your GUI is probably working but your strlife is not being updated for some reason.
try this one ins$$anonymous$$d see what that does... it will debug lifes.. so we know for sure its updating.
void Update(){
strc = numc.ToString();
strh = numh.ToString();
strp = nump.ToString();
strlif = lifes.ToString();
//be sure to have the "collapse" option checked in the console!!
Debug.Log(lifes);
}
also make sure you don't have any red errors.. they halt the script execution...
update is once per frame, OnGUI is multiple/per event. As a test, put lifes.ToString() in the label and replace the variable strlif, and evaluate what is happening then.
@Fornoreason1000 no one is arguing that, but we are trying to isolate the issue.