Advertisement
LeeMace

Game over / Restart / Pause

Jan 3rd, 2023 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7.  
  8. public class GameManager : MonoBehaviour
  9. {
  10. //pausescreen is a UI panel
  11.     [SerializeField] GameObject pauseScreen;
  12.     private bool paused;
  13.     //deactivate the gameobjects in the inspector
  14.     public TextMeshProUGUI gameOverText;
  15.     //remember to assign the on click event in the inspector. Drag in the game manager and choose the restart function.
  16.     public Button restartButton;
  17.     public bool isGameActive;
  18.  
  19.     private void Start()
  20.     {
  21.         isGameActive = true;
  22.     }
  23.     // Update is called once per frame
  24.     void Update()
  25.     {
  26.         if(Input.GetKeyDown(KeyCode.P))
  27.         {
  28.             PauseGame();
  29.         }
  30.     }
  31.  
  32.     void PauseGame()
  33.     {
  34.         if (!paused)
  35.         {
  36.             paused = true;
  37.             pauseScreen.SetActive(true);
  38.             Time.timeScale = 0;
  39.         }
  40.         else
  41.         {
  42.             paused = false;
  43.             pauseScreen.SetActive(false);
  44.             Time.timeScale = 1;
  45.         }
  46.     }
  47.  
  48.     public void UpdateLives(int livesToChange)
  49.     {
  50.         lives += livesToChange;
  51.         livesText.text = "Lives: " + lives;
  52.         if (lives <= 0)
  53.         {
  54.            GameOver();
  55.         }
  56.     }
  57.  
  58.     public void GameOver()
  59.     {
  60.         gameOverText.gameObject.SetActive(true);
  61.         isGameActive = false;
  62.         restartButton.gameObject.SetActive(true);
  63.     }
  64.  
  65.     public void RestartGame()
  66.     {
  67.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  68.     }
  69.  
  70.  public void ReturnToMainMenu()
  71.     {
  72.         SceneManager.LoadScene(0);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement