Answer the question
In order to leave comments, you need to log in
How to make the defeat screen correctly?
Good day, I made a defeat screen in the canvas when the player has zero health (he appears). I had a problem with the destruction of the enemy (when his health is <0, he does not disappear from the game, and his health goes to minus and an error appears saying that the object is not assigned) since the health script is also attached to it, here is the health script:
public class Health : MonoBehaviour
{
public GameObject GameOverScreen;
[SerializeField] private int health;
public Action <int , GameObject> OnTakeHit;
public int CurrentHealth
{
get{return health;}
}
public int health1
{
get {return health;}
set
{
if (value >200)
health =value;
}
}
private void Start()
{
GameManager.Instance.healthContainer.Add(gameObject,this);
GameManager.Instance.collisionContainer.Add(gameObject,this);
}
public void TakeHit(int damage,GameObject attacker)
{
health -= damage;
if(OnTakeHit!=null)
OnTakeHit(damage, attacker);
if (health <= 0)
{
if (!GameOverScreen.activeSelf)
{
GameOverScreen.SetActive(true);
GetComponent<Player>().speed = 0;
}
health = 0;
Destroy(gameObject);
Time.timeScale = 0;
}
}
Answer the question
In order to leave comments, you need to log in
Well, just write a condition that if the player died, then turn on the defeat screen, if the enemy - then not. To do this, either create a separate script for the player and enemies, or, when calling the TakeHit method, specify who exactly calls it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question