Created
June 16, 2022 12:09
-
-
Save Mazday21/cdabb3778cda81a6b148a3ecd421dadf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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