Advertisement
coding_giants

l16 BricksScript

Mar 26th, 2024
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BricksScript : MonoBehaviour
  6. {
  7.     //A private variable that can be specified in the inspector
  8.     //The amount of life of our brick
  9.    
  10.     public int life = 1;
  11.  
  12.  
  13.     //Every time she comes into contact with something
  14.     private void OnCollisionEnter(Collision collision)
  15.     {
  16.         //If it has a Ball tag
  17.         if(collision.gameObject.tag == "Ball")
  18.         {
  19.             //we are removing one life
  20.             life--;
  21.             BrickColor();
  22.             //If lives are equal to or less than 0
  23.             if (life <= 0)
  24.             {
  25.                 GameManager.instance.bricks.Remove(this.gameObject);
  26.                 GameManager.instance.UpdateUI();
  27.                 Destroy(gameObject); //destroy object
  28.             }
  29.         }
  30.     }
  31.  
  32.     public void SetBrick(int life)
  33.     {
  34.         this.life = life;
  35.         if (life <= 0) Destroy(gameObject);
  36.         BrickColor();
  37.     }
  38.  
  39.     void BrickColor()
  40.     {
  41.         Renderer renderer = GetComponent<Renderer>();
  42.         Color[] colors = { Color.white, Color.blue, Color.red, Color.green };
  43.         int colorIndex = Mathf.Clamp(life - 1, 0, colors.Length-1);
  44.         renderer.material.color = colors[colorIndex];
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement