unity 2d desplay lives left code example

Example: unity 2d desplay lives left

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LivesSystem : MonoBehaviour
{

    public GameObject[] playerLives;
    public int currentLives;
    public int maxLives;
    // Start is called before the first frame update
    void Start()
    {
        CheckLives();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void CheckLives()
    {
        if (currentLives == maxLives)
        {
            playerLives[0].gameObject.SetActive(true);
            playerLives[1].gameObject.SetActive(true);
            playerLives[2].gameObject.SetActive(true);
            playerLives[3].gameObject.SetActive(true);
            playerLives[4].gameObject.SetActive(true);
        }
        else if (currentLives == 4)
        {
            playerLives[0].gameObject.SetActive(true);
            playerLives[1].gameObject.SetActive(true);
            playerLives[2].gameObject.SetActive(true);
            playerLives[3].gameObject.SetActive(true);
            playerLives[4].gameObject.SetActive(false);
        }
        else if (currentLives == 3)
        {
            playerLives[0].gameObject.SetActive(true);
            playerLives[1].gameObject.SetActive(true);
            playerLives[2].gameObject.SetActive(true);
            playerLives[3].gameObject.SetActive(false);
            playerLives[4].gameObject.SetActive(false);
        }
        else if (currentLives == 2)
        {
            playerLives[0].gameObject.SetActive(true);
            playerLives[1].gameObject.SetActive(true);
            playerLives[2].gameObject.SetActive(false);
            playerLives[3].gameObject.SetActive(false);
            playerLives[4].gameObject.SetActive(false);
        }
        else if (currentLives == 1)
        {
            playerLives[0].gameObject.SetActive(true);
            playerLives[1].gameObject.SetActive(false);
            playerLives[2].gameObject.SetActive(false);
            playerLives[3].gameObject.SetActive(false);
            playerLives[4].gameObject.SetActive(false);
        }
    }
}

Tags:

Misc Example