Hi All,
I have my player with three lives. So if the player gets hurt once, he dies. Once the player runs out of lives, i’d like the level to restart. Thank you for your tips and tricks.
UIController Script :
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class UIController : MonoBehaviour
- {
- public static UIController instance;
- public Image life1, life2, life3;
- public Sprite lifeFull, LifeEmpty;
- private void Awake()
- {
- instance = this;
- }
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- public void UpdateHealthDisplay()
- {
- switch (PlayerHealthController.instance.currentHealth)
- {
- case 3:
- life1.sprite = lifeFull;
- life2.sprite = lifeFull;
- life3.sprite = lifeFull;
- break;
- case 2:
- life1.sprite = lifeFull;
- life2.sprite = lifeFull;
- life3.sprite = LifeEmpty;
- break;
- case 1:
- life1.sprite = lifeFull;
- life2.sprite = LifeEmpty;
- life3.sprite = LifeEmpty;
- break;
- case 0:
- life1.sprite = LifeEmpty;
- life2.sprite = LifeEmpty;
- life3.sprite = LifeEmpty;
- break;
- }
- }
- }
Level Manager Script:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LevelManager : MonoBehaviour
- {
- public static LevelManager instance;
- public float waitToRespawn;
- private void Awake()
- {
- instance = this;
- }
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- public void RespwanPlayer ()
- {
- StartCoroutine(RespawnCo());
- }
- private IEnumerator RespawnCo ()
- {
- PlayerController.instance.gameObject.SetActive(false);
- PlayerController.instance.theSR.flipX = false;
- yield return new WaitForSeconds(waitToRespawn);
- PlayerController.instance.gameObject.SetActive(true);
- PlayerController.instance.transform.position = CheckController.instance.spawnPoint;
- PlayerController.instance.gameObject.SetActive(true);
- }
- }
Player Health Controller Script:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerHealthController : MonoBehaviour
- {
- public static PlayerHealthController instance;
- public int currentHealth, maxHealth;
- public void Awake()
- {
- instance = this;
- }
- // Start is called before the first frame update
- void Start()
- {
- currentHealth = maxHealth;
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- public void DealDamage()
- {
- currentHealth–; //-- means take away 1
- if (currentHealth <= 0)
- {
- LevelManager.instance.RespwanPlayer();
- }
- UIController.instance.UpdateHealthDisplay();
- }
- }