I’ve written a script that deducts lives every time that my object collides with a ball, however, the object does not get destroyed when it reaches 0 but, instead, has to be hit one more time at zero to be destroyed. I would like the object to be destroyed when the lives reach 0 and not have to be hit one more time. Here is my script:
using UnityEngine;
using System.Collections;
public class RedBase : MonoBehaviour {
void OnCollisionEnter (Collision col) {
RedLives();
}
public void RedLives() {
if(Lives.redCurLives > 0)
Lives.redCurLives--;
else {
GameOver();
}
}
public void GameOver() {
Destroy(gameObject) ;
}
}
Change RedLives() to this:
public void RedLives() {
if (Lives.redCurLives > 0)
Lives.redCurLives--;
if (Lives.redCurLives == 0)
GameOver();
}
After changing Lives.redCurLives you need to check if it hit zero.
public void RedLives() {
Lives.redCurLives–;
if(Lives.redCurLives <= 0){
Lives.redCurLives = 0;
Destroy(gameObject)
}
}
IMHO,
I used
if(Lives.redCurLives < 1)
{
GameOver()
}
and set the var to float.