- Home /
irregular decrease of the lives in health control
hi guys i have a health control script thats have some problem,when i play the game its working fine by minus 1 lives but when i replay the game its minus 3 lives or even 6 lives. i do not know why i suspect is because i have 2 different damage style that make the health control script crash, one is fallout the other one is bullet.
here my health control script:
var health3 : Texture2D; var health2 : Texture2D; var health1 : Texture2D;
var player : Transform;
static var Lives = 3; private var playerInfo : ThirdPersonStatus;
// Cache link to player's state management script for later use.
function Awake() { playerInfo = FindObjectOfType(ThirdPersonStatus); if (!playerInfo) Debug.Log("No link to player's state manager."); }
function Update() { print("Lives: "+Lives+" Hits: "+HITS); switch(Lives) { case 3: guiTexture.texture = health3; break; case 2: guiTexture.texture = health2; break; case 1: guiTexture.texture = health1; break; case 0: Application.LoadLevel("3"); break; } and here my thirpersonststus:
function LateUpdate() { if(dead) { transform.position = Vector3(639,39,427); gameObject.Find("Main Camera").transform.position = Vector3(639,39,-427); dead = false; } }
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "fallout") { dead = true; HealthControl.Lives -= 1; }
else if(hit.gameObject.tag == "bomgy 1")
{
dead = true;
HealthControl.Lives -= 1;
}
if(hit.gameObject.tag == "spike")
{
dead = true;
HealthControl.Lives -=1;
}
}
please fix your code formatting, there is a code block button ( icon is 0s and 1s )
var Lives = 3;
var Score : int = 0;
static var playerScore : int;
//static var HITS = false;
static var dead = false;
// sound effects. var struckSound: AudioClip; var deathSound: AudioClip;
var Script : HealthControl;
private var heart = false;
var bullitPrefab : Transform;
function LateUpdate() { if(dead) { transform.position = Vector3(639,39,427); gameObject.Find("$$anonymous$$ain Camera").transform.position = Vector3(639,39,-427); dead = false; } }
function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "fallout") { dead = true; HealthControl.Lives -= 1; }
else if(hit.gameObject.tag == "bomgy 1")
{
dead = true;
HealthControl.Lives -= 1;
}
if(hit.gameObject.tag == "spike")
{
dead = true;
HealthControl.Lives -=1;
}
what you mean by not setting anywhere?how to make Lives to be equal 3?
Answer by Kacer · Sep 12, 2011 at 10:58 AM
If you're replaying the game by using the "application.loadlevel" then the lives wont get reset to 3 or whatever your default value is.
Because.
you're using a static variable as the lives, Static variables dont get reset between level loads because thats the way they work (there's some longer explanation as to why, but i really cant remember it >_<), what you can do in the "start function" of this script, maybe "awake" even, is to set lives to their default value, that should fix it.
thanks but i don't understand what you mean? how to i write the function awake to? is it from the the health control or from the thirdpersonstatus or the loadapplication script.Can show me an example?thank you
try writing this in the beginning of the script where your health variable is declared:
function Start(){
Lives = 3;
}
thank you for the example i solve already one last question how do i reset back the Lives back to 3 when i replay the game?