Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resetting Level

Discussion in '2D' started by TheAPGames, Jul 21, 2020.

  1. TheAPGames

    TheAPGames

    Joined:
    Jun 1, 2020
    Posts:
    44
    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 :

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class UIController : MonoBehaviour
    6. {
    7. public static UIController instance;
    8. public Image life1, life2, life3;
    9. public Sprite lifeFull, LifeEmpty;
    10. private void Awake()
    11. {
    12. instance = this;
    13. }
    14. // Start is called before the first frame update
    15. void Start()
    16. {
    17. }
    18. // Update is called once per frame
    19. void Update()
    20. {
    21. }
    22. public void UpdateHealthDisplay()
    23. {
    24. switch (PlayerHealthController.instance.currentHealth)
    25. {
    26. case 3:
    27. life1.sprite = lifeFull;
    28. life2.sprite = lifeFull;
    29. life3.sprite = lifeFull;
    30. break;
    31. case 2:
    32. life1.sprite = lifeFull;
    33. life2.sprite = lifeFull;
    34. life3.sprite = LifeEmpty;
    35. break;
    36. case 1:
    37. life1.sprite = lifeFull;
    38. life2.sprite = LifeEmpty;
    39. life3.sprite = LifeEmpty;
    40. break;
    41. case 0:
    42. life1.sprite = LifeEmpty;
    43. life2.sprite = LifeEmpty;
    44. life3.sprite = LifeEmpty;
    45. break;
    46. }
    47. }
    48. }
    Level Manager Script:


    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class LevelManager : MonoBehaviour
    5. {
    6. public static LevelManager instance;
    7. public float waitToRespawn;
    8. private void Awake()
    9. {
    10. instance = this;
    11. }
    12. // Start is called before the first frame update
    13. void Start()
    14. {
    15. }
    16. // Update is called once per frame
    17. void Update()
    18. {
    19. }
    20. public void RespwanPlayer ()
    21. {
    22. StartCoroutine(RespawnCo());
    23. }
    24. private IEnumerator RespawnCo ()
    25. {
    26. PlayerController.instance.gameObject.SetActive(false);
    27. PlayerController.instance.theSR.flipX = false;
    28. yield return new WaitForSeconds(waitToRespawn);
    29. PlayerController.instance.gameObject.SetActive(true);
    30. PlayerController.instance.transform.position = CheckController.instance.spawnPoint;
    31. PlayerController.instance.gameObject.SetActive(true);
    32. }
    33. }



    Player Health Controller Script:


    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerHealthController : MonoBehaviour
    5. {
    6. public static PlayerHealthController instance;
    7. public int currentHealth, maxHealth;
    8. public void Awake()
    9. {
    10. instance = this;
    11. }
    12. // Start is called before the first frame update
    13. void Start()
    14. {
    15. currentHealth = maxHealth;
    16. }
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. }
    21. public void DealDamage()
    22. {
    23. currentHealth--; //-- means take away 1
    24. if (currentHealth <= 0)
    25. {
    26. LevelManager.instance.RespwanPlayer();
    27. }
    28. UIController.instance.UpdateHealthDisplay();
    29. }
    30. }
     
  2. Daniel7Mazi

    Daniel7Mazi

    Joined:
    Jul 15, 2020
    Posts:
    8
    Add this line below to use scene management
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    Then when you check your player die put this line
    Code (CSharp):
    1. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    This should help you.