- Home /
Variables in GUI not updating/changing? (javascript)
I'm working in javascript and attempting to make a very simple battle system. The issue is, I have to declare the players' and monsters' health in an if statement (because there are different player classes and monsters), but whenever I do this once the calculations in the if statement are done (monsterHealth -= playerAttack) the monster health doesn't update because the condition of the if statement (the current monster's original stats) is still active. Does anyone know how I might fix this? (the problem happens both with the playerHealth and the monsterHealth).
This is the class for the monster stats:
#pragma strict
public class MonsterData extends MonoBehaviour
{
static public var monsterHealth : int;
static public var monsterArmor: int;
static public var monsterAttack : int;
static public var curMonster : String;
static public var totDamage1 : int = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
static public var totDamage2 : int = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
static public var monNum : Monster = new Monster(0);
public class Monster
{
public function Monster(t : int)
{
totDamage1 = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
totDamage2 = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
if(t == 1)
{
curMonster = "Mutated Wolf";
monsterHealth = 16;
monsterArmor = 2;
monsterAttack = (Random.Range(2, 4));
}
}
}
}
And this is the GUI that runs the battle:
if(GameGUI.gamestage == -201)
{
if(GameGUI.backNum == 4)
{
GUI.skin = nightText;
}
else
{
GUI.skin = text;
}
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 300, 500, 500), Mon.curMonster);
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 250, 500, 500), "" + Mon.monsterHealth);
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 200, 500, 500), "Me");
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 250, 500, 500), "" + PlayerStats.playerHealth);
if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 100, 500, 35), PlayerStats.attack1))
{
Mon.monsterHealth -= Mon.totDamage1;
PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
Mon.monsterAttack = (Random.Range(2, 4));
}
if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 50, 500, 35), PlayerStats.attack2))
{
Mon.monsterHealth -= Mon.totDamage2;
PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
Mon.monsterAttack = (Random.Range(2, 4));
}
if(Mon.monsterHealth <= 0)
{
Mon.monsterHealth = 0;
Debug.Log("0");
}
if(PlayerStats.playerHealth == 0)
{
GameGUI.gamestage = -3;
}
}
Hi bgreal5! Is there any particular reason why you have nested classes and static variables? Static variables can be a pain to work with if you are not sure how they work. If I understand you correctly you have several instances of $$anonymous$$onster, right? Since the variables are static they don't really belong to the instance but to the class. So there is only one monsterHealth variable in the entire application domain. When you change the value, you don't isolate the change to a single instance.
But if there's a good reason for using statics that I didn't grasp from your question, I apologise.
I was using static because the GUI that runs the battle is in a different script and I can't access the variables from the battle runner if I don't make them static. Would you suggest I move the GUI runner to the same script as the monster class so I don't have to deal with static?
No, don't do that. You should seperate them completely.
Ok, so you have a bunch of $$anonymous$$onster instances that you want to perform actions on from you GUI. You have some alternatives to accomplish this.
First of all, lose the statics and the nested class.
If you have set number of monsters the easiest way is to declare a public array of monsters in you GUI class. Then you can just drag and drop your monster GameObjects from the hierarchy to the array on the GUI.
Another alternative is to work with delegates and events. You'll get cleaner and more abstract code.
If you are spawning $$anonymous$$onsters you should check out object pooling. Simply instantiate say 50 monster at startup, and spawn and despawn using SetActive(false) and SetActive(true).
Good luck!
Thanks! I'll be sure to try it out and come back to update with results!
Answer by BenDurch · Apr 19, 2014 at 12:50 AM
this works for me.
var Bug : GameObject;
var Boom : Transform;
var dammage : int = 10;
var health : float = 100;
private var Dead : boolean;
function OnTriggerEnter2D (col : Collider2D)
{
children = GetComponentsInChildren.<Renderer>();
if (col.tag == "Bullet"){
health+=-dammage;
}
}
function Update (){
if (health<0){
Dead = true;
}
if (Dead==true){
Destroy(Bug,0);
}
}
Your answer
Follow this Question
Related Questions
Simple quest system: from Update function to OnGUI? (javascript) 0 Answers
problem deforming mesh colliders 1 Answer
Eliminating input loss 1 Answer
GUI opacity. 1 Answer
Unity Controller Support Help 0 Answers