Advertisement
LeeMace

Lives

Dec 8th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | Gaming | 0 0
  1. //in game manager.cs
  2.  //for lives UI
  3.     public TextMeshProUGUI livesText;
  4.     private int lives;
  5.  
  6.  public void UpdateLives(int livesToChange)
  7.     {
  8.         lives += livesToChange;
  9.         livesText.text = "Lives " + lives;
  10.         if(lives<= 0)
  11.         {
  12.             GameOver();
  13.         }
  14.  
  15.     }
  16.  
  17.  public void StartGame(int difficulty)
  18.     {
  19.      //starting number of lives
  20.         UpdateLives(5);
  21.     }
  22.    
  23.     //In Target.cs script
  24.    
  25.      private GameManager gameManager;
  26.      
  27.       void Start()
  28.     {
  29.      gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
  30.     }
  31.    
  32.     //object hitting sensor destroys it
  33.     //1 life is taekn away if object hits the sensor
  34.     private void OnTriggerEnter(Collider other)
  35.     {
  36.         Destroy(gameObject);
  37.         if (!gameObject.CompareTag("Bad"))
  38.             {
  39.             gameManager.UpdateLives(-1);
  40.             if (gameManager.lives < 0)
  41.                 {
  42.                 gameManager.lives = 0;
  43.                 gameManager.UpdateLives(0);
  44.                 }  
  45.             }
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement