Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class LifeManager : MonoBehaviour
- {
- //public int startingLives; // How many lives the player starts with
- private int lifeCounter; // How many lives the player has
- private Text theText; // Reference to the Text component of the object
- public GameObject gameOverScreen; // Reference to the game over screen object
- public PlayerController player; // Reference to the player
- private bool isGameOver = false; // To track if the game is over
- public string mainMenu; // Reference to the main menu scene name
- public float waitAfterGameOver;// Time to wait after the game over screen is activated
- // Start is called before the first frame update
- void Start()
- {
- theText = GetComponent<Text>();// Get the Text component
- lifeCounter = PlayerPrefs.GetInt("PlayerCurrentLives"); // Get the player's lives from the PlayerPrefs
- theText.text = "Lives: " + lifeCounter; // Set the player's lives text
- player = FindObjectOfType<PlayerController>(); // Find the player
- }
- // Update is called once per frame
- void Update()
- {
- if (lifeCounter <= 0) // If the player's lives are less than 0
- {
- gameOverScreen.SetActive(true); // Activate the game over screen
- player.gameObject.SetActive(false); // Deactivate the player next i to the game over screen
- Debug.Log("isGameOver: " + isGameOver);// Add this line to check if isGameOver is being called
- isGameOver = true; // Set the game over flag
- Debug.Log("isGameOver: " + isGameOver);// Add this line to check if isGameOver is being called
- }
- theText.text = "x " + lifeCounter; // Update the player's lives text
- if(gameOverScreen.activeSelf) // If the game over screen is active
- {
- waitAfterGameOver -= Time.deltaTime; // Subtract Time.deltaTime from waitAfterGameOver
- if(waitAfterGameOver <= 0) // If waitAfterGameOver is less than or equal to 0
- {
- SceneManager.LoadScene(mainMenu); // Load the main menu
- }
- }
- }
- public void AddLife() // Add a life
- {
- if (!isGameOver) // Only add lives if the game isn't over
- {
- lifeCounter++; // Add a life
- PlayerPrefs.SetInt("PlayerCurrentLives", lifeCounter); // Set the player's lives in the PlayerPrefs
- }
- }
- // Take a life when the player dies currently taking 2 lives upon the first death.
- public void TakeLife() // Take a life when the player dies currently taking 2 lives upon the first death.
- {
- Debug.Log("lifeCounter -= 1 is called"); // Add this line to check if lifeCounter -= 1 is being called
- if (!isGameOver) // Only take a life if the game isn't over
- {
- lifeCounter--; // Take a life
- PlayerPrefs.SetInt("PlayerCurrentLives", lifeCounter); // Set the player's lives in the PlayerPrefs
- Debug.Log("lifeCounter -= 1 is called"); // Add this line to check if lifeCounter -= 1 is being called
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement