Made the system lives on this tutorial. At the end (video time 28.15), when I transferred the Main HUD to the prefab and other levels, an error occurred. Namely, on the first level, everything is fine, but when you go to the second or further, error pops up and life (other lives that are hearts on top) and glasses are not displayed.

View c of the game:

enter image description here

View from console:

enter image description here

LifeManager script code:

using UnityEngine; using System.Collections; using UnityEngine.UI; public class LifeManager : MonoBehaviour { public int startingLives; private int lifeCounter; private Text theText; public GameObject gameOverScreen; public PlayerController player; public string mainMenu; public float waitAfterGameOver; // Use this for initialization void Start () { theText = GetComponent<Text> (); lifeCounter = startingLives; player = FindObjectOfType<PlayerController>(); } // Update is called once per frame void Update () { if (lifeCounter < 0) { gameOverScreen.SetActive(true); player.gameObject.SetActive(false); } theText.text = "x " + lifeCounter; if (gameOverScreen.activeSelf) { waitAfterGameOver -= Time.deltaTime; } if (waitAfterGameOver < 0) { Application.LoadLevel(mainMenu); } } public void GiveLife() { lifeCounter++; } public void TakeLife() { lifeCounter--; } } 

HealthManager Script Code:

 using UnityEngine; using System.Collections; using UnityEngine.UI; public class HealthManager : MonoBehaviour { public int maxPlayerHealth; public static int playerCurHealth; private LevelManeger levelManager; private LifeManager lifeSystem; // Use this for initialization void Start () { playerCurHealth = maxPlayerHealth; levelManager = FindObjectOfType<LevelManeger>(); lifeSystem = FindObjectOfType<LifeManager>(); } // Update is called once per frame void Update () { if (playerCurHealth <= 0) { playerCurHealth = 0; levelManager.RespawnPlayer(); lifeSystem.TakeLife(); } if (playerCurHealth > maxPlayerHealth) { playerCurHealth = maxPlayerHealth; } } public static void HurtPlayer(int damageToGive) { playerCurHealth -= damageToGive; } public void FullHealth() { playerCurHealth = maxPlayerHealth; } } 

HUD script code:

 using UnityEngine; using System.Collections; using UnityEngine.UI; public class HUD : MonoBehaviour { public Sprite[] HertSprites; public Image HeartUI; //private HealthManager helthManager; public PlayerController player;//был private void Start() { player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>(); //helthManager = FindObjectOfType<HealthManager>(); } void Update() { HeartUI.sprite = HertSprites[HealthManager.playerCurHealth]; } 

A couple of organizational issues:

1. Not all parts of the code may coincide with the tutorial because you had to make changes.

2. My project . To reproduce the error, you need to remove the Canvas from the second level and replace it with the Main HUD prefab.

3.If you die with Game Over, then through the snails.

  • one
    HeartUI.sprite = HertSprites[HealthManager.playerCurHealth]; , NRE type exception. According to the code, I don’t see where these variables come from, so I advise you to read, for example, ru.stackoverflow.com/questions/413041/… - Monk
  • @Monk if there is not enough information, then you can check out the project. Link provided in the post below. And for your link arigato, the other day I read. - Artik Slayer

1 answer 1

If you look at the editor for the HUD script, which hangs on the Main Camera object, you can see that the two properties are not filled. Correcting this omission eliminates the error described.

  • One property should be empty (player), and the second UI apparently disappeared when renamed. You are mega. - Artik Slayer