Y
Y
Yuri2021-07-10 16:15:54
Canvas
Yuri, 2021-07-10 16:15:54

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

I had an option to write a separate health script for the enemy, but since all my health depends on the GameManager, this option does not suit me ... I need the enemy to be destroyed without depending on the defeat screen object. Can you suggest how to fix this misunderstanding?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnanasikDev, 2021-07-11
@uriy99

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 question

Ask a Question

731 491 924 answers to any question