player is destroyed before it can process empty hearts

My player has a heart system in the top left where, whenever they get collide with an enemy, one of their three hearts turns into an empty heart and if they get hit three times, they are destroyed. Unfortunately, on the third hit the player gets destroyed before it can process the third heart becoming empty, which looks really weird. At least I think this is what’s happening. I am very new to Unity so I’m sure it’s a simple fix.

Here are my codes for the player health and the enemy behavior, respectively.

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
public int numOfHearts;
public int hitpoints;

public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;

public GameObject playerDie;

void Update ()
{
	if(hitpoints > numOfHearts)
	{
		hitpoints = numOfHearts;
	}

	for (int i = 0; i < hearts.Length; i++)
	{
		if(i < hitpoints)
		{
			hearts*.sprite = fullHeart;*
  •   	} else*
    
  •   	{*
    

_ hearts*.sprite = emptyHeart;_
_
}*_

* if(i < numOfHearts)*
* {*
_ hearts*.enabled = true;
} else*
* {
hearts.enabled = false;
}
}
}
public void TakeHit(int damage)
{
hitpoints -= damage;
if(hitpoints <= 0)
{
Destroy(gameObject);
GameObject effect = Instantiate(playerDie, transform.position, Quaternion.identity);
Destroy(effect, 1f);
}
}
} *

using System.Collections;
using System.Collections.Generic;
using UnityEngine;_

public class EnemyFollow : MonoBehaviour
{

* public float speed;*
* public GameObject enemyDie;*
* private Transform target;*

* void Start()*
* {*

* target = GameObject.FindGameObjectWithTag(“Player”).GetComponent();*

* }*

* void Update()*
* {*

transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

* }*

* void OnCollisionEnter2D(Collision2D collision)*
* {*
* var player = collision.collider.GetComponent();*
* if (player)*
* {*
* player.TakeHit(1);*
* Destroy(gameObject);*
* GameObject effect = Instantiate(enemyDie, transform.position, Quaternion.identity);*
* Destroy(effect, 1f); *
* }*
* }*
}

I would advise you to change the way you update the hearts, you are making the game run needless code every frame on the Update function, you don’t want the game asking itself “Hey player have you lost a health this last second??” every second…

Instead try to add the code where you update the hearts to the TakeHit function, something like:

public void TakeHit(int damage)
 {
     hitpoints -= damage;
     for (int i = 0; i < hearts.Length; i++)
     {
         if(i < hitpoints)
         {
             hearts*.sprite = fullHeart;*

} else
{
hearts*.sprite = emptyHeart;*
}
}
if(hitpoints <= 0)
{
Destroy(gameObject);
GameObject effect = Instantiate(playerDie, transform.position, Quaternion.identity);
Destroy(effect, 1f);
}
}
For the rest of the code you have on the update function i think i could probably be on a start function, as it doesn’t seem to change along the game.
Hope i could help you m8 :wink: