- Home /
problem with enable/disable on health script
so I have a modified version of my health script that a user in the community had edited for me in a different question. The script is pretty much fine but I am having issues with the enable and disable parts of the scritpt. What I wanted to do is when the player dies it will temporarily disable and when the player respawns it will enable again. However when the player dies nothing happens the health variable continues to take damage and end up on a negative number. Is there a better way to achieve the same or similar results?
here is the script
var PointCube: Transform;
var spawneffect: Transform;
var deatheffect: Transform;
var SpawnPoints: Transform[];
private var SpawnPoint: Transform;
//Respawn timer for the coroutine
var respawnTime: float = 3;
var respawn: boolean = false;
//This is the players current health
var health: float = 100;
//The max health the player is spawned with
var healthText: UI.Text;
//At start set the players health to maxHealth
function Start() {
health = 100;
}
function Damage(dmg: float) {
health -= dmg;
}
function Update() {
if (healthText) {
healthText.text = health.ToString();
// If the players health is zero start the respawn function
if (health <= 0) {
yield Respawn();
}
}
}
//This is a coroutine. Basically meaning it has a special wait timer function
function Respawn() {
//temporarily disable player
gameObject.SetActive (false);
Instantiate(deatheffect, transform.position, transform.rotation);
Instantiate(PointCube, transform.position, transform.rotation);
//Wait for the respawnTimer. (This is the special wait timer I was speaking of)
yield WaitForSeconds(respawnTime);
//Find a random spawn point
var spawnPointID: float = Random.Range(0, SpawnPoints.length);
//Set the player at that random spawn point
transform.position = SpawnPoints[spawnPointID].position;
//enable player
gameObject.SetActive (true);
Instantiate(spawneffect, transform.position, transform.rotation);
var audio: AudioSource = GetComponent.<AudioSource>();
audio.Play();
audio.Play(44100);
//change this to set float not add to it
health = 100;
}
Answer by fafase · Jan 17, 2018 at 07:45 AM
Make sure the value is never negative:
function Damage(dmg: float)
{
health -= dmg;
if (health < 0) { health = 0; }
}
But are you sure your code works? Coz you are disabling the game object so the coroutine won't run.
You 'd need to disable the rendering and collider only
@fafase ahh I see, the same thing the other guy on my last question said I'll give that a try but is there a way I can disable the player's camera child object from this script so it would hide the player's camera so the game can display the spectating camera or is there no need to do this ?
I'd say you don't need to disable the camera. Just unparent it from the player, then place it where it should be. When restarting your player, parent it back where it was.
void Death(){
Camera.main.transform.parent = null;
Camera.main.transform.position = spectatingposition;
}
void Rebirth()
{
Camera.main.transform.parent = player.transform;
Camera.main.transform.position = eyePosition;
}
I think I found the problem. I think it has something to do with the wait event, because I want it to wait 3 seconds before continueing the rest of the script but it seems to be negating the rest of the script
Your answer
Follow this Question
Related Questions
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
Most effective way to get deactivated children? 1 Answer
How to activate the child of an object without declaring a public transform? 1 Answer
How To Disable A Prefab? 2 Answers
ReActivate object with null movement ?? 2 Answers