Advertisement
johnnygoodguy2000

LifeManager.cs

May 12th, 2024 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class LifeManager : MonoBehaviour
  8. {
  9.     //public int startingLives; // How many lives the player starts with
  10.     private int lifeCounter; // How many lives the player has
  11.     private Text theText; // Reference to the Text component of the object
  12.     public GameObject gameOverScreen; // Reference to the game over screen object
  13.     public PlayerController player; // Reference to the player
  14.     private bool isGameOver = false; // To track if the game is over
  15.  
  16.     public string mainMenu; // Reference to the main menu scene name
  17.  
  18.     public float waitAfterGameOver;// Time to wait after the game over screen is activated
  19.  
  20.     // Start is called before the first frame update
  21.     void Start()
  22.     {
  23.         theText = GetComponent<Text>();// Get the Text component
  24.         lifeCounter = PlayerPrefs.GetInt("PlayerCurrentLives"); // Get the player's lives from the PlayerPrefs
  25.         theText.text = "Lives: " + lifeCounter; // Set the player's lives text
  26.         player = FindObjectOfType<PlayerController>(); // Find the player
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if (lifeCounter <= 0) // If the player's lives are less than 0
  33.         {
  34.            
  35.             gameOverScreen.SetActive(true); // Activate the game over screen
  36.            
  37.             player.gameObject.SetActive(false); // Deactivate the player next i to the game over screen
  38.            
  39.             Debug.Log("isGameOver: " + isGameOver);// Add this line to check if isGameOver is being called
  40.             isGameOver = true; // Set the game over flag
  41.             Debug.Log("isGameOver: " + isGameOver);// Add this line to check if isGameOver is being called
  42.            
  43.            
  44.  
  45.          
  46.            
  47.         }
  48.         theText.text = "x  " + lifeCounter; // Update the player's lives text
  49.  
  50.         if(gameOverScreen.activeSelf) // If the game over screen is active
  51.         {
  52.             waitAfterGameOver -= Time.deltaTime; // Subtract Time.deltaTime from waitAfterGameOver
  53.  
  54.             if(waitAfterGameOver <= 0) // If waitAfterGameOver is less than or equal to 0
  55.             {
  56.                 SceneManager.LoadScene(mainMenu); // Load the main menu
  57.             }
  58.         }
  59.     }
  60.  
  61.     public void AddLife() // Add a life
  62.     {
  63.  
  64.  
  65.         if (!isGameOver) // Only add lives if the game isn't over
  66.         {
  67.             lifeCounter++; // Add a life
  68.             PlayerPrefs.SetInt("PlayerCurrentLives", lifeCounter); // Set the player's lives in the PlayerPrefs
  69.         }
  70.     }
  71.     // Take a life when the player dies currently taking  2 lives upon the first death.
  72.  
  73.     public void TakeLife() // Take a life when the player dies currently taking  2 lives upon the first death.
  74.     {
  75.         Debug.Log("lifeCounter -= 1 is called"); // Add this line to check if lifeCounter -= 1 is being called
  76.         if (!isGameOver) // Only take a life if the game isn't over
  77.         {
  78.             lifeCounter--; // Take a life
  79.             PlayerPrefs.SetInt("PlayerCurrentLives", lifeCounter); // Set the player's lives in the PlayerPrefs
  80.             Debug.Log("lifeCounter -= 1 is called"); // Add this line to check if lifeCounter -= 1 is being called
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement