- Home /
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[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if(i < numofhearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].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<GameMan>();
}
}
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
After i die and respawn
Is the playerPrefab that you're Instantiating in RespawnPlayer()
what has the HealthSystem component attached? If so, perhaps double check the actual prefab (and not what you're editing in the hierarchy) and make sure it's setup correctly. There's always a chance you edited the reference in the hierarchy and it isn't saved to the prefab.
Answer by GrayLightGames · Nov 11, 2019 at 05:09 AM
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.
Also, don't die! :) Hang in there, there's always a solution!