- Home /
displaying text problem
I have made this script for my splitscreen tank game, where if the player looses all five lives their tank prefab would be disabled, I have set up a text variable for both the remaining health and lives, but for some reason when the tank runs out of lives and is disabled the text doesn't change to 0. Has it got something to do with the disabling of the tank or is it something else?
here is the script var spawneffect : Transform; var deatheffect : Transform; var SpawnPoint : Transform; var respawn : boolean = false; var health : float = 90; var lives : float = 5; var outoflives : boolean = false; var gotlives : boolean = true; var healthtext : UI.Text; var livestext : UI.Text;
function Damage(dmg:float){
health -= dmg;
}
function Update (){
if (healthtext)healthtext.text = health.ToString();
if (livestext)livestext.text = lives.ToString();
if (gotlives){
if(health <=0){
Instantiate (deatheffect,transform.position,transform.rotation);
transform.position = SpawnPoint.position;
Instantiate (spawneffect,transform.position,transform.rotation);
var audio: AudioSource = GetComponent.<AudioSource>();
audio.Play();
audio.Play(44100);
lives -= 1;
health += 100;
}
}
if (lives <=0){
outoflives = true;
gotlives = false;
}
}
Answer by Larry-Dietz · Dec 24, 2017 at 04:35 AM
From looking at your script, I would expect the text to be displaying 0 Lives and 100 Health after losing your final life.
Is this what you are seeing?
If this is what you are seeing, and you are expecting/wanting Health to also be showing 0, then in your if Health block, where you are setting Health back to 100, just change it to the code below. Currently when your health dropped to or below 0 on the final life, and it decrements lives to 0, it is also setting health to 100.
if(lives>)
health+=100;
Hope this helps, -Larry
@Larry-Dietz well to clarify my question, when the tank loses it's final life, I want it to be able to display 0 on both the lives and health bar, but at the moment when the tank loses it's final life and is disabled it doesn't change the life bar text to zero, the health text stays on zero like it should but the life bar text remains on one even though the life float has been reduced to 0, my guess it's something to do with disabling the tank so the script is unable to change the text.
I don't see the code that is disabling the tank. If it is getting disabled before the update statement runs again after lives hits 0, then you are right. That would cause exactly what you are describing.
$$anonymous$$y first thought would be that since you are disabling the tank when lives hit 0, wherever that disabling is happening just explicitly set that text object to 0 at that point.
-Larry