Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity-Breakout Game with a Healthbar

Discussion in 'Scripting' started by KDamon, Oct 20, 2017.

  1. KDamon

    KDamon

    Joined:
    Sep 21, 2017
    Posts:
    33
    I'm making a breakout game, and I wanted to add a healthbar that decreases when the ball touches a certain object with the tag "hazard". I have a Game Manager Script, and A Pickup interact script, but with the way I set it up, I'm kind of confused with how to trigger takedamage from my GM script to my pickup script, considering I put my playerhealth elements into my GM script, so I can attach it to empty gameobject call Game Manager, since the actual player isnt in the hierarchy, but instantiated during runtime. I'm hoping I don't have to redo the whole thing just for this purpose. If someone could help me figure this out, I'd appreciate it.

    Here's my GM script

    Code (CSharp):
    1.  
    2.  
    3. public class GM : MonoBehaviour
    4. {
    5.  
    6.    public int lives = 3;
    7.    public int bricks = 20;
    8.    public float resetDelay = 1f;
    9.    public Text livesText;
    10.  
    11.    public GameObject gameOver;
    12.    private GameObject clonePaddle;
    13.    public GameObject youWon;
    14.    public GameObject bricksPrefab;
    15.    public GameObject paddle;
    16.    public GameObject deathParticles;
    17.    public static GM instance = null;
    18.  
    19.    public int startingHealth = 100;
    20.    public int currentHealth;
    21.    public Slider healthSlider;
    22.  
    23.    bool isDead;
    24.    bool damaged;
    25.  
    26.  
    27.  
    28.    void Awake()
    29.    {
    30.        currentHealth = startingHealth;
    31.        TakeDamage(10);
    32.  
    33.        if (instance == null)
    34.            instance = this;
    35.        else if (instance != this)
    36.            Destroy(gameObject);
    37.  
    38.        Setup();
    39.  
    40.    }
    41.  
    42.    public void TakeDamage(int amount)
    43.    {
    44.        damaged = true;
    45.  
    46.        currentHealth -= amount;
    47.        healthSlider.value = currentHealth;
    48.  
    49.        if (currentHealth <= 0)
    50.        {
    51.            LoseLife();
    52.        }
    53.  
    54.    }
    55.  
    56.    public void Setup()
    57.    {
    58.        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    59.        Instantiate(bricksPrefab, transform.position, Quaternion.identity);
    60.    }
    61.  
    62.  
    63.    void CheckGameOver()
    64.    {
    65.        if (bricks < 1)
    66.        {
    67.            youWon.SetActive(true);
    68.            Time.timeScale = .25f;
    69.            Invoke("Reset", resetDelay);
    70.        }
    71.  
    72.        if (lives < 1)
    73.        {
    74.            gameOver.SetActive(true);
    75.            Time.timeScale = .25f;
    76.            Invoke("Reset", resetDelay);
    77.        }
    78.  
    79.    }
    80.  
    81.    void Reset()
    82.    {
    83.        Time.timeScale = 1f;
    84.        Application.LoadLevel(Application.loadedLevel);
    85.    }
    86.  
    87.    public void LoseLife()
    88.    {
    89.        lives--;
    90.        livesText.text = "Lives: " + lives;
    91.        Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
    92.        Destroy(clonePaddle);
    93.        Invoke("SetupPaddle", resetDelay);
    94.        CheckGameOver();
    95.    }
    96.  
    97.    void SetupPaddle()
    98.    {
    99.        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    100.    }
    101.  
    102.    public void DestroyBrick()
    103.    {
    104.        bricks--;
    105.        CheckGameOver();
    106.    }
    107. }
    108.  

    And Here's my Pickup Script:

    Code (CSharp):
    1.  
    2.  
    3. public class Pickups : MonoBehaviour {
    4.  
    5.    public float PaddleSpeedValue = 0.5f;
    6.  
    7.    private bool isActive = false;
    8.  
    9.    public float thrust=20f;
    10.  
    11.    public Rigidbody rb;
    12.  
    13.    GameObject player;
    14.  
    15.  
    16.  
    17.    private void OnTriggerEnter(Collider other)
    18.    {
    19.         if (other.tag == "Hazard")
    20.        {
    21.            isActive = true;
    22.            Destroy(other.gameObject);
    23.        }
    24.    }
    25.  
    26. }
     
  2. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    187
    Make the healthbar and make it a singleton too or use a delegate (UnityAction) in GM and register on it from the healthbar (if the healthbar cant be a singleton or cant contain static elements)

    You could even kill it by making "health" a static in the player, because i think you cant have multible health values at once right?
    Then you can read that static from the healthbar just representing that value
     
    Last edited: Oct 20, 2017
  3. ghostmode

    ghostmode

    Joined:
    Sep 26, 2016
    Posts:
    14
    Your game manager is a singleton, so you can access it anywhere via it's static 'instance' field. For example in your OnTriggerEnter code:

    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.tag == "Hazard")
    4.     {
    5.         GM.instance.TakeDamage(10);
    6.  
    7.         isActive = true;
    8.         Destroy(other.gameObject);
    9.     }
    10. }