Search Unity

Power Up Slider Help!

Discussion in 'UGUI & TextMesh Pro' started by lizzielewis, Nov 18, 2022.

  1. lizzielewis

    lizzielewis

    Joined:
    Aug 25, 2022
    Posts:
    16
    Hi! I am a Unity& code beginner. I am developing a simple 2D platform for a class and I am running into difficulty creating a slider that is affected when my player runs through a power-up. I have a script attached to my power-up that destroys the power-up when it is ran through. I also have a script attached to my slider. Now the issue I am experiencing is how to link the slider to the action of the player colliding with a power-up. I want the slider to increase in value every time my player collides with a power-up. Can anyone tell me what I am doing wrong? Here is the code I have so far:

    Power-up script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Collectible : MonoBehaviour
    {
    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.tag == "Player")
    {
    Destroy(this.gameObject);
    }

    }
    }


    Slider Script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class HealthBar : MonoBehaviour
    {
    public Slider slider;
    public void SetMaxHealth(int health)
    {
    slider.maxValue = health;
    slider.value = health;
    }
    public void SetHealth(int health)
    {
    slider.value = health;
    }

    }
     
  2. Sixbreaker

    Sixbreaker

    Joined:
    Dec 29, 2016
    Posts:
    11
    you can use transform.gameObject.GetComponent<>() to grab the object that is a parent of the Collider2D other.

    I added a SubtractHealth() function to update the slider value. To increase, you can use AddHealth()

    Alternatively, you can use GameObject.Find(" ") to access the object such as a player to obtain its components


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Collectible : MonoBehaviour
    7. {
    8.    private void OnTriggerEnter2D(Collider2D other)
    9.   {
    10.     if (other.tag == "Player") {
    11.        other.transform.gameObject.GetComponent<HealthBar>().SubtractHealth();    
    12.     }
    13.   }
    14. }
    15.  
    16.  
    17. //Slider Script:
    18.  
    19. using System.Collections;
    20. using System.Collections.Generic;
    21. using UnityEngine;
    22. using UnityEngine.UI;
    23.  
    24. public class HealthBar : MonoBehaviour
    25. {
    26.   public Slider slider;
    27.  
    28.   public void SetMaxHealth(int health)
    29.   {
    30.     slider.maxValue = health;
    31.     slider.value = health;
    32.   }
    33.  
    34. public void SetHealth(int health)
    35. {
    36.    slider.value = health;
    37. }
    38.  
    39. public void SubtractHealth() {
    40.    slider.value -= 0.1f;
    41. }
    42. }
     
    Last edited: Nov 19, 2022
  3. lizzielewis

    lizzielewis

    Joined:
    Aug 25, 2022
    Posts:
    16
    Thank you! I tried this but when I try it out in game mode, it says "object reference not set to the instance of an object" and the slider does not move. Is there something I should add?
     
  4. Sixbreaker

    Sixbreaker

    Joined:
    Dec 29, 2016
    Posts:
    11
    "also have a script attached to my slider. Now the issue I am experiencing is how to link the slider to the action of the player colliding with a power-up"

    is the script class HealthBar attached to the player? and the slider is not "None" in inspector?

     
    Last edited: Nov 19, 2022
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So you know, the 2D forum isn't for posts related to the UI system so I'll move your post to the UI forum. Also, when posting code, please use code-tags and not plain text as it makes it much easier to read code and refer to it by line number.

    Thanks.
     
  6. lizzielewis

    lizzielewis

    Joined:
    Aug 25, 2022
    Posts:
    16
    The healthBar is attached to my UI slider and is named HealthBar in the inspector
     
  7. lizzielewis

    lizzielewis

    Joined:
    Aug 25, 2022
    Posts:
    16
    Sorry, first time posting in the forums, wasn't sure.
     
    MelvMay likes this.
  8. Sixbreaker

    Sixbreaker

    Joined:
    Dec 29, 2016
    Posts:
    11
    I recommend that the best solution is to attach the HealthBar script to the player. Your UI slider can be referenced here:

    Code (CSharp):
    1. public class HealthBar : MonoBehaviour
    2. {
    3.   public Slider slider; // drag your ui slider to this variable on the player inspector
    4.  
    5.   // ...
    6. }


    alternatively, you can do this also if you want to maintain HealthBar on the UI object

    there's many ways; choose the best one that fits your goal, but you have to provide reference to your scripts either inspector or using GetComponent<>()

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Collectible : MonoBehaviour
    8. {
    9.  
    10.    public HealthBar hb;
    11.  
    12.    void Start ( ) {
    13.           // if the UI name is Slider, otherwise provide the "name"
    14.           hb = GameObject.Find("Slider").GetComponent<HealthBar>();
    15.    }
    16.  
    17.    private void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.     if (other.tag == "Player") {
    20.           // call your script functions from class HealthBar
    21.           hb.AddHealth( )
    22.     }
    23.   }
    24. }
     
    Last edited: Nov 19, 2022
    lizzielewis likes this.
  9. lizzielewis

    lizzielewis

    Joined:
    Aug 25, 2022
    Posts:
    16
    You are amazing!!!! The first solution was exactly what I was looking for. Thank you so much for your patience & your help.