Skip to content

Instantly share code, notes, and snippets.

@Mazday21
Created June 16, 2022 12:09
Show Gist options
  • Save Mazday21/cdabb3778cda81a6b148a3ecd421dadf to your computer and use it in GitHub Desktop.
Save Mazday21/cdabb3778cda81a6b148a3ecd421dadf to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
[RequireComponent(typeof(CanvasGroup))]
public class GameOver : MonoBehaviour
{
[SerializeField] private Player _player;
[SerializeField] private Button _restartButton;
private CanvasGroup _gameOverGroup;
private Animator _animator;
private void Start()
{
_animator = _player.GetComponent<Animator>();
_gameOverGroup = GetComponent<CanvasGroup>();
_gameOverGroup.alpha = 0;
}
private void OnEnable()
{
_player.Died += OnGameOvered;
}
private void OnDisable()
{
_player.Died -= OnGameOvered;
}
private void OnGameOvered()
{
_restartButton.onClick.AddListener(OnRestartButtonClick);
_animator.SetBool("Died", true);
_gameOverGroup.alpha = 1;
Time.timeScale = 0;
}
private void OnRestartButtonClick()
{
Time.timeScale = 1;
SceneManager.LoadScene(0);
_restartButton.onClick.RemoveListener(OnRestartButtonClick);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment