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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Healthbar - fillAmount from image does not work, null reference to an object, but I initialize it.

Discussion in 'Scripting' started by HolyNova, May 20, 2015.

Thread Status:
Not open for further replies.
  1. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    Hi all,

    I would like to kindly ask for a help with problem I got today. I tried to implement a classic health bar UI element using new UI, but I can't get it work. Unity is telling me, that my Image component is null, but I don't have it empty and I try to initialize this Image in the start of the script. Before I hit play button, everything looks fine, but as soon as I start playing the game, image is gone. Why? Could someone please have a look and help me?

    Thank you very much for help

    My script handling all the healthbar is here: http://pastebin.com/8rXivEiU
    Here is, how it actually looks in unity: http://prntscr.com/77alfv
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. visualHealth = GetComponent<Image>();
    3.  
    this gets the image component from the gameobject the script is attached to, as you call this in start it overwrites whatever you have put in the inspector... the image you want is on a child object.

    So either, remove the GetComponent from the start function and assign in the inspector. OR find the correct gameobject first and then get the image component from that object in the start function.



    also, looks like you've not used the UI.Slider object... ?
     
  3. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    Hello Lefty,

    that is just awesome how quickly you reply. Thanks again!

    I see, now I get it. Problem which rises, when I delete GetComponent is, that immediately after the start of the game, my healthBar is going to zero, not taking in account the damage player got. Here's the picture how it looks after 2-3 seconds in game. http://prntscr.com/77arnq (Originally, bar should be filled somewhere around 94 +-). Looks to me like it is taking a 0 value in the beggining of the game, but can't figure out where. Console does not provide any error. Any idea please?

    I did not use slider. Only things I am using is two images for BG, one as a vbar itself and then a UI.text. Is it better for such a things to use slider?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    slider just already has the code for resizing the child fill rect; when set to interactable=false and removing the "handle" child and resetting a few of the anchor values you can just do slider.value = x; and it'll act just like a health bar would. If you've already got the code for doing that there isn't really any gain in switching over i don't think :)


    Code (csharp):
    1.  
    2. Mathf.Lerp(visualHealth.fillAmount, currentValue / playerStats.Health, Time.deltaTime);
    3.  
    lerp is just used for figuring out some midpoint between two values. It's often used in movement in conjunction with Time.deltaTime for working out how far you need to move this frame (hence the deltaTime). For your case you have no interest in time. I think you need:

    Code (csharp):
    1.  
    2. Mathf.Lerp(0, playerStats.Health, currentValue / playerStats.Health);
    3.  
     
  5. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    Actually, I think, that deltaTime in here
    Code (CSharp):
    1.  
    2. Mathf.Lerp(visualHealth.fillAmount, currentValue / playerStats.Health, Time.deltaTime);
    3.  
    is responsible for how quick is the bar going to move, after player is hit. For example from value 90 to value 80 (well in regards for FillAmount from 0.9 to 0.8). That is why I need it. If the time is not here, or if it is big number, then it changes it instantly and does not look good. And the first value is actual value of fillAmount, which is at the start equal to 1. Then the second value is a value in which it will be changing. I debugged the solution again and found the problem.

    It was in this line:
    Code (CSharp):
    1. currentHealth = playerStats.Health;
    After I completed all the operations, I assigned currentValue value of playerStats.Health, therefore, all the time they were both the same, so Lerp calculation was always 1 (Lerp(1, 1, time) = 1. So the healthbar was not moving, because fillAmount was always one.

    Anyway thanks for help :)
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh, it's animated, missed that :oops:

    *goes to get more coffeeeeee *
     
  7. VladBonciu

    VladBonciu

    Joined:
    Jul 13, 2019
    Posts:
    1
    never came back...
     
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Please don't Necro a 5 year old thread for no reason, it's against the Community Code of Conduct. If you have a problem similar to the issue, just create a new thread describing your issue instead of pointlessly bumping this one.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.