Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Slider is not instantiating correctly nor is automatically updating

Discussion in 'Scripting' started by brdavid_unity, Apr 2, 2020.

  1. brdavid_unity

    brdavid_unity

    Joined:
    Mar 4, 2020
    Posts:
    2
    I'm using a slider to act as a health bar for an enemy. I have a prefab that is basically a canvas with a slider inside of it. I link this prefab to my enemy inside a script called Healthbar. Inside Healthbar I have the following code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBar : MonoBehaviour
    7. {
    8.     [SerializeField] GameObject HealthBarObject;
    9.     [SerializeField] float MaxHealth;
    10.     [SerializeField] float CurrentHealth;
    11.     Slider SlideObj;
    12.  
    13.     public float GetMaxHealth() { return MaxHealth; }
    14.     public float GetCurrentHealth() { return CurrentHealth; }
    15.     public void AdjustCurrentHealth(float val) { CurrentHealth -= val; }
    16.     public void SetCurrentHealth(float val) { CurrentHealth = val; }
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         Vector3 point = Camera.main.WorldToScreenPoint(new Vector3(transform.position.x, transform.position.y, transform.position.z));
    22.         point = new Vector2(point.x, point.y + 45);
    23.         var sliderCanvas = Instantiate(HealthBarObject, new Vector2(0, 0), Quaternion.identity);
    24.         var sliderObj = sliderCanvas.transform.Find("Slider");
    25.         sliderObj.transform.position = point;
    26.  
    27.         SlideObj = HealthBarObject.GetComponentInChildren<Slider>();
    28.         SlideObj.minValue = 0.0f;
    29.         SlideObj.maxValue = 1.0f;
    30.         SlideObj.value = 1.0f;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         SlideObj.value = SlideObj.value - (Time.deltaTime * 0.1f);
    37.     }
    All I am doing is automatically decreasing the slider value to test.

    The problem is this does not work. The slider will not change. Moreover when I stop the game and start it again, the health instantiates with an updated value. I have included two screen shots: one where the health bar is full and another where the health bar is depleted. The problem here is that the depleted health bar is where the slider starts. When I start a new game the slider starts where it left off previously. How is that possible if I am instantiating a whole new canvas and slider? Shouldn't it always reset to max?
     

    Attached Files:

  2. brdavid_unity

    brdavid_unity

    Joined:
    Mar 4, 2020
    Posts:
    2
    I figured this out. Line 23 I instantiate my object but I am going after the prefab, not the instantiated object which I should be referencing since that is what I want to change, that is what is on the screen.

    Silly mistake.
     
  3. loldilenarda

    loldilenarda

    Joined:
    Jun 5, 2020
    Posts:
    1
    hi david, I'm actually having the same problem right now. I completely understand where the problem lies (and your reply makes total sense), however I'm not sure how I could use the instantiated object in the code instead of the prefab itself.

    Could you please help with how you've done it?