My arrays automatically empty out there objects.

this is a 2d game, with a health system. the code is used for the the health system is this;

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

public class HealthSystem : MonoBehaviour
{

public int health;
public int numofhearts;

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

private void Update()
{
    if (health > numofhearts)
    {
        health = numofhearts;
    }
    for (int i = 0; i < hearts.Length; i++)
    {
        if (i < health)
        {
            hearts*.sprite = fullHeart;*

}
else
{
hearts*.sprite = emptyHeart;*
}
if(i < numofhearts)
{
hearts*.enabled = true;*
}
else
{
hearts*.enabled = false;*
}
}

}
}
But when i fill up the array with the images its works until i respawn using these 2 codes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{

public HealthSystem healthSystem;
private HealthSystem health;

void Update()
{

if(transform.position.y<= -20)
{
DamagePlayer(20);
}

}

public void DamagePlayer(int damage)
{
healthSystem.health -= damage;
if (healthSystem.health <= 0)
{
GameMan.killPlayer(this);
}
}
}
And;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameMan : MonoBehaviour
{
private HealthSystem health;
public HealthSystem healthSystem;
void Update()
{

}
public GameObject Enter;
public void EnterIn()
{
Enter.SetActive(true);
}
public static GameMan gm;
void Start()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag(“GameMan”).GetComponent();
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public int spawnDelay = 2;
public IEnumerator RespawnPlayer()
{
yield return new WaitForSeconds(spawnDelay);
Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);

}

public static void killPlayer(Player player)
{
Destroy(player.gameObject);
gm.StartCoroutine(gm.RespawnPlayer());
}
}
Please help me fix these problem, thank you
Before i die
[148563-screen-shot-2019-11-10-at-93252-pm.jpg|148563]
After i die and respawn
[148564-screen-shot-2019-11-10-at-93425-pm.jpg*|148564]*
*
*

Hi @ipk4fun_, I noticed in your screenshot that the images you are linking to are bold in the inspector link box… this indicates a difference between the object and the prefab. Select the prefab itself and look what is there in the list. I am guessing that they are all None there because you can’t link prefab variables to objects outside the prefab. So because the prefab has an empty list, so will the player when you instantiate!

If I’m right, here’s an option for you: Store and manage the hearts list in a different object that is never destroyed. There’s no reason to store the health info on the player… and if you do for some reason you don’t need to store the list of images. When the player respawns or is damaged, you can call a function on the Healthsystem object to update the heart container images based on the health and the number of hearts. If you store a reference to the Healthsystem on GameMan, you should be able to access it from either GameMan or the Player. And you can reset health or num of hearts in the respawn function.
Hope that helps! I can elaborate if you need, but I’m pretty sure that is the source of the issue.