Need script for simple heath system
So I've been working on a Space Shooter game and for now each time my ship gets hit,my ship is destroyed. So I'm looking for some help regarding a simple script where my Ship would have say 5 health and each time it gets hit the health decreases by 1. I'm not asking for health bar GUI or something, just a simple script when attached to my ship and when any collider hits my ship it would loose 1 unit of health.
Here is my collider code so far but the update() method is not allowing me to put OnTrigger inside it. Some time the script is working for 2 sec but not everytime health returns to 5 automatically .
public class ColliderContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; private Done_GameController gameController; public int Health = 5;
void Update()
{
Debug.Log(Health);
}
void Start ()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <Done_GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Boundary" || other.tag == "Enemy")
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
Health--;
gameController.GameOver();
}
}
gameController.AddScore(scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
Answer by yaourt · Jun 19, 2019 at 12:49 PM
Is this script attached to your Player? If so, why are you trying to detect collision on a "Player" tag? What are those lonely brackets inside the condition? Why do you call gameController.GameOver()
on each collision? Does this method reset your health?
The script is attached to an empty object named gamecontroller not with the player. And the GameOver() is from another script that only enables (Boolean) when the ship is destroyed, asu$$anonymous$$g now the player has 1 unit of health. And yes this method is resting my health. I was following the Space Shooter project from unity to set the explosion and other things so they had these.
First thing, if this script is attached on an empty gameObject, you are trying to detect collision on an empty gameObject without any collider, so no collision neither... OnTriggerEnter will trigger only if there is a collision between the collider attached on the same gameobject than your script and an other one. One of them has to have a RigidBody attached (most part of the time it will be the player). In your case, you should probably attached this script on the player (which as health) and detect collision with Enemies, so add an enemy tag to your enemy gameObject and check for this tag on collision. If you don't understand any line you are writing, I highly recommend you to start, with an ultra-simple 2d game, or if you really really don't understand, with basics of programmation.