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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to display my lives using UI in my PlayerMovement class?

Discussion in 'Immediate Mode GUI (IMGUI)' started by JediJaimie, Mar 31, 2015.

  1. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    I don't understand how UI works really well, I'm trying to display my lives, and try to see if my simple lives system is working. I did is UI thing before, In my ScoreManager Class when displaying the score. but In my dead OnTriggerEnter and lives is in the playermovent class but I did a Coroutine with the die and spawn system in my GameManagerClass, I know I cant put the PlayerMovement script in the text component. Do i have to make a playerdead class with lives or do this in my GameManager? I did a simple UI with my ScoreManager and it work when i put the ScoreManager in the text component. Here's the scripts of my PlayerMovement, GameManager.

    PlayerMovement
    Code (CSharp):
    1. //References
    2.     public static int Lives = 3;
    3.     public GameManager gameManager;
    4.  
    5. void Start ()
    6.     {
    7.         gameManager = FindObjectOfType<GameManager> ();
    8.  
    9.     }
    10.  
    11. void OnTriggerEnter2D (Collider2D other)
    12.     {
    13.         if (other.transform.tag == "Enemy")
    14.         {
    15.             PlayerMovement.Lives--;
    16.             Debug.Log("Lost a Life");
    17.             gameManager.RespawnPlayer();
    18.         }
    19.      
    20.     }
    21.  
    GameManager
    Code (CSharp):
    1. public GameObject currentCheckpoint;
    2.     private PlayerMovement player;
    3.     public float respawnDelay;
    4.  
    5.  
    6. public void RespawnPlayer()
    7.     {
    8.  
    9.         StartCoroutine ("RespawnPlayerCo");
    10.     }
    11.      
    12.  
    13.  
    14.     public IEnumerator RespawnPlayerCo()
    15.     {
    16.         player.enabled = false;
    17.         player.renderer.enabled = false;
    18.         player.rigidbody2D.isKinematic = true;
    19.         player.transform.position = currentCheckpoint.transform.position;
    20.         yield return new WaitForSeconds (respawnDelay);
    21.         player.enabled = true;
    22.         player.renderer.enabled = true;
    23.         player.rigidbody2D.isKinematic = false;
    24.         Debug.Log("Respawn");
    25.     }
    26. }
     
  2. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10