Day 80 — Implementing Lives and Respawning

public Text livesText;public void UpdateLives(int lives)
{
livesText.text = "Lives: " + lives;
}
using UnityEngine.SceneManagement;public GameObject _startPosition;
private int _lives = 3;
private UIManager _uiManager;
void Start()
{
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_lives--;
_uiManager.UpdateLives(_lives);
CharacterController cc = other.GetComponent<CharacterController>();
if (cc != null)
{
cc.enabled = false;
StartCoroutine(CCEnableRoutine(cc));
}
other.transform.position = _startPosition.transform.position; if (_lives < 1)
{
SceneManager.LoadScene(0);
}
}
}
IEnumerator CCEnableRoutine(CharacterController controller)
{
yield return new WaitForSeconds(0.5f);
controller.enabled = true;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store