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);
}
}