- Home /
GUI player menu is not updating values (score, exp, HPs)
GUI player menu is not updating values (score, exp, HPs) after I added Hit Points and Exp to character (name of prefab:Player (yeah, I know - how original)). I'll try to describe everything, but the line that's generates problem (not error - everything is working but not as I designed) is in second line of code beneath point six:
One. (number list makes formating here weird and is messing with code view) I created cube prefab (name:Cube);
Two. I created script that I added to my player (tag: "Player" (PlayerStatus.js):
public var actualHealth : int = 100;
public var maximumHealth : int = 100;
public var lives : int = 3;
public var actualEXP : int = 0;
public var actualLVL : int = 0;
function ApplyDamage (damage : int)
{
actualHealth -=damage;
if (actualHealth<= 0)
{
SendMessage("Die");
lives --;
}
}
function ApplyEXP (exp : int)
{
actualEXP +=exp;
if(actualEXP >= 100)
{
actualEXP -= 100;
actualLVL++;
}
}
Three. I created script that after collision send signal that something happened and attached it to cube prefab (I call it CubeExperience.js - it basically should add exp and take hitpoints by var damage and disappear in thin air):
public var exp : int= 45;
public var damage : int= 10;
public var hit : RaycastHit;
public function OnTriggerEnter (hit : Collider) : IEnumerator
{
if (hit.collider.tag == "Player")
{
hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
hit.collider.SendMessage("ApplyEXP", exp, SendMessageOptions.DontRequireReceiver);
Destroy (this.gameObject);
}
}
Four. Everything was right as you can see during gameplay that values of PlayerStatus are changing (and it can be added to anything so it nice and everything, but...). There's always a "but(t)"...
Five. I wanted to implement pause function in my HeroMenu.js and I found it easy though I must disabled player (person beghind keyboard not my prefab) controls because when player presses space or arrow keys, after closing menu hero was jumping which could lead to his death:
public function Update () : void
{
if (Input.GetKey(KeyCode.M))
{
if (b_openMenu == false)
{
//pausing game
Time.timeScale = 0;
//opening menu
b_openMenu = true;
//disabling player movement script
var playerMovement = GetComponent("CharacterController_2D");
playerMovement.enabled = false;
}
}
}
private function DoMyWindow (windowID : int) : void
{
in_toolbar = GUI.Toolbar(r_tabButton, in_toolbar, s_toolbars, GUI.skin.GetStyle("Tab Button"));
switch (in_toolbar)
{
case 0 : //status page
StatusWindow();
break;
case 1 : //items page
ItemWindow();
break;
case 2 : //equipment page
EquipmentWindow();
break;
}
GUI.DrawTexture (r_hero, t_hero); //Draw our background character texture
if (GUI.Button (r_closeButton, "", GUI.skin.GetStyle("Exit Button")))
{
b_openMenu = false;
Time.timeScale = 1;
var playerMovement = GetComponent("CharacterController_2D");
playerMovement.enabled = true;
}
GUI.DragWindow();
}
Six. After that, full of enthusiasm I was trying to change both HP, EXP and other RPG like values (score, Attack, etc.) values but after I failed in all I just concentrated my efforts on EXP, so I change HeroMenu.js script (that's my GUI script) (I pasted only the crucial/changed parts) cause I didn't want make any trouble, but I can also paste entire script or send it:
private var playerInfo : PlayersStatus;
private var showEXP = playerInfo.actualEXP; //<-this line
//generates NullReferenceException: Object reference not set to an instance of
//an object. HeroMenu..ctor () (at Assets/HeroMenu.js:110)
private function StatusWindow() : void
{
GUI.Box (r_statBox, "");
GUI.Box (r_weaponBox, "");
GUI.DrawTexture(r_statTexture1, t_statusBox1);
GUI.DrawTexture(r_statTexture2, t_statusBox2);
GUI.DrawTexture(r_skillBox, t_skillBox);
CheckMax();
GUI.Label (r_hpLabel, currentHP.ToString() + "/" + fullHP.ToString(), "Text Amount");
GUI.Label (r_mpLabel, currentMP.ToString() + "/" + fullMP.ToString(), "Text Amount");
GUI.Label (r_lvLabel, currentLV.ToString(), "Text Amount");
GUI.Label (r_expLabel, showEXP.ToString(), "Text Amount");
GUI.Label (r_nextLabel, currentNEXT.ToString(), "Text Amount");
GUI.Label (r_atkLabel, currentATK.ToString(), "Text Amount");
GUI.Label (r_defLabel, currentDEF.ToString(), "Text Amount");
GUI.Label (r_agiLabel, currentAGI.ToString(), "Text Amount");
GUI.Label (r_intLabel, currentINT.ToString(), "Text Amount");
GUI.Label (r_lucLabel, currentLUC.ToString(), "Text Amount");
GUI.Label (r_weaponLabel, gui_weaponCon, "Text Item");
GUI.Label (r_armorLabel, gui_armorCon, "Text Item");
GUI.Label (r_accessLabel, gui_accessCon, "Text Item");
GUI.Label (r_skillTexture, gui_skillCon, "Text Item");
}
private function CheckMax () : void
{
fullHP = Mathf.Clamp(fullHP, 0.0, maxHP);
fullMP = Mathf.Clamp(fullMP, 0.0, maxMP);
currentHP = Mathf.Clamp(currentHP, 0.0, fullHP);
currentMP = Mathf.Clamp(currentMP, 0.0, fullMP);
currentLV = Mathf.Clamp(currentLV, 0.0, maxLV);
showEXP = Mathf.Clamp(showEXP, 0.0, maxEXP);
currentNEXT = Mathf.Clamp(currentNEXT, 0.0, maxNEXT);
currentATK = Mathf.Clamp(currentATK, 0.0, maxATK);
currentDEF = Mathf.Clamp(currentDEF, 0.0, maxDEF);
currentAGI = Mathf.Clamp(currentAGI, 0.0, maxAGI);
currentINT = Mathf.Clamp(currentINT, 0.0, maxINT);
currentLUC = Mathf.Clamp(currentLUC, 0.0, maxLUC);
}
Seven. At some point I even add this to Start() in HeroMenu.js
//if(!playerInfo) //{ // playerInfo = GameObject.Find("Player").GetComponent(PlayersStatus); //}
(but I commented this as it was changing nothing)
Eight. I changed many times many variables I even was trying to sending exp : int directly to HeroMenu.js (private function StatusWindow(exp : int)) but it was generating more errors. I hope that I didn't bore you all but I was trying to describe everything for future references.
Nine. Thank you all for your time.
Here on answers.unity3d I found this: http://answers.unity3d.com/questions/228803/Updating-GUILabel-for-Score.html This is exactly my problem in a little shorter way but those answers are not solving it.
Answer by McDardy · Aug 29, 2012 at 06:19 AM
So... the answer found me in person of Jate Wittayabundit (yup! That's right! Author of Unity 3 Game Development Hotshot)
"For your question, you need to get the playerInfo before calling it. Then, when you want to update the EXP number inside the StatusWindow().
Like this : in HeroMenu.js.
private var playerInfo : PlayersStatus;
private var showEXP : int = 0;
function Start()
{
playerInfo = GameObject.Find("Player").GetComponent(PlayersStatus); //<-this was added
}
private function StatusWindow() : void
{
// Update the actual EXP
showEXP = playerInfo.actualEXP; //<- this is making the difference
GUI.Box (r_statBox, "");
GUI.Box (r_weaponBox, "");
GUI.DrawTexture(r_statTexture1, t_statusBox1);
GUI.DrawTexture(r_statTexture2, t_statusBox2);
GUI.DrawTexture(r_skillBox, t_skillBox);
CheckMax();
GUI.Label (r_expLabel, showEXP.ToString(), "Text Amount");
}
"
I removed all of not necessary lines to not confusing anybody with answer but if something is unclear just comment me for help.
And thanks Jate one more time.