Working on a battle system, GUI not updating with newest values...

Here is my battle.js code:

var currentGUIMethod : Function;//Which GUI we are using

function Start () {
 this.currentGUIMethod = mainMenu;
}

function Update () {

}

function mainMenu(){
 GUILayout.BeginArea(Rect(Screen.width * 0.25, Screen.height * 0.025, Screen.width * 0.5, Screen.height * 0.95));//Begin GUI Area
 GUILayout.Window (0, Rect (Screen.width * 0.01, Screen.height * 0.69, Screen.width * 0.98, Screen.height * 0.3), Menu, "Main Menu");// Window ID, Dimensions, Function, Title
 GUILayout.EndArea();//End GUI Area
}

function Menu (){ 
 var player : Player;
 for(player in Game.playerList){ 
 GUILayout.BeginHorizontal();
 GUILayout.BeginVertical ("box");
 if(GUILayout.Button ("Attack")) {
 player.playerHealth = player.playerHealth - player.playerStrength;
 }
 if(GUILayout.Button ("Skills")){
 
 }
 if(GUILayout.Button ("Summon")){
 
 }
 if(GUILayout.Button ("Items")) {
 
 }
 GUILayout.EndVertical ();
 GUILayout.BeginVertical("box");
 
 GUILayout.Label(player.playerName);
 GUILayout.Label((player.playerHealthString)+"/"+(player.playerMaxHealthString));
 GUILayout.Label((player.playerActionPointsString)+"/"+(player.playerMaxActionPointsString));
 }
 // GUILayout.Label(Game.playerCharacter.playerName);
 // GUILayout.Label(Game.playerCharacter.playerPortrait);
 // GUILayout.Label((Game.playerCharacter.playerHealthString)+(Game.playerCharacter.playerMaxHealthString));
 // GUILayout.Label((Game.playerCharacter.playerActionPointsString)+(Game.playerCharacter.playerMaxActionPointsString));
 GUILayout.EndVertical();
 GUILayout.EndHorizontal();
}

function OnGUI () {
 this.currentGUIMethod();//Draw the current GUI
}

Here is the script containing the player values:

static var playerCharacter : Player;
static var playerList = Array();
var tempPlayer = Player();

function Awake () {
 createPlayerList();
    DontDestroyOnLoad (transform.gameObject);
}

function Update () {
// player = GameObject.Find("Player");
// playerPos = player.transform.position;
 //Debug.Log(itemList.length); 
 if(playerCharacter.playerHealth <= 0){
 Debug.Log("Game Over");
 }
}

function createPlayerList (){
 playerCharacter = Player();
 playerCharacter.player = GameObject.Find("Player");
 playerCharacter.playerPortrait = Resources.Load ("Textures/player/portrait", Texture);
 playerCharacter.playerBattleTexture = Resources.Load ("Textures/player/battletexture", Texture);
 playerCharacter.playerName = "Clare";
 playerCharacter.playerHealth = 150;
 playerCharacter.playerMaxHealth = 150;
 playerCharacter.playerActionPoints = 50;
 playerCharacter.playerMaxActionPoints = 50;
 playerCharacter.playerStrength = 5;
 playerCharacter.playerDefense = 6;
 playerCharacter.playerKarma = 0;
 playerCharacter.playerMoney = 500;
 playerCharacter.playerHealthString = playerCharacter.playerHealth.ToString();
 playerCharacter.playerMaxHealthString = playerCharacter.playerMaxHealth.ToString();
 playerCharacter.playerActionPointsString = playerCharacter.playerActionPoints.ToString();
 playerCharacter.playerMaxActionPointsString = playerCharacter.playerMaxActionPoints.ToString();
 playerCharacter.playerKarmaString = playerCharacter.playerKarma.ToString();
 playerCharacter.playerMoneyString = playerCharacter.playerMoney.ToString();
 playerList.Push(playerCharacter);
}

When I click attack it deals the damage to the playerCharacter, I know this because when the playerCharacter’s health reaches 0 the Console prints “Game Over”

What I’m wondering (and cant seem figure out) is why the GUI doesn’t update to reflect the player’s health everytime?

Can you see what’s going wrong?

Solved!

On creating and submitting the player information to the Array in the player script, I was telling it:

 playerCharacter.playerHealthString = playerCharacter.playerHealth.ToString();

This seems to have been what was keeping the values the same, it wasn’t updating with the Battle.js script

So, what I did was:

for(player in Game.playerList){
displayPlayerName = player.playerName;
displayPlayerHealth = player.playerHealth.ToString();
displayMaxPlayerHealth = player.playerMaxHealthString;
displayPlayerActionPoints = player.playerActionPointsString;
displayMaxPlayerActionPoints = player.playerMaxActionPointsString;
}

So instead of relying on old information in the array that wasn’t being updated, I told it to convert the string on the fly in the Battle.js script

=D