Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Image not changing with .fillAmount

Discussion in 'Scripting' started by SrAranha, Mar 15, 2022.

  1. SrAranha

    SrAranha

    Joined:
    Jan 26, 2022
    Posts:
    5
    I'm making an bossbar that instantiate the gameobject in UI and gets destroyed when the enemy dies, but the problem is the image wich represents the enemy's health isn't changing visually, but the fillAmount as show by the debug log is changing.
    And a bonus problem is the instantiated bossbar isn't getting destroyed with the enemy.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BossBar : MonoBehaviour
    7. {
    8.     public string bossName;
    9.     public GameObject bossBar;
    10.     public GameObject UI;
    11.  
    12.     bool activeBar = false;
    13.     EnemyController c_enemy;
    14.     Text bossbarName;
    15.     // TODO: pegar a imagem correta
    16.     Image bossbarHealth;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         c_enemy = GetComponent<EnemyController>();
    22.         bossbarHealth = bossBar.GetComponentInChildren<Image>();
    23.         Debug.Log(bossbarHealth); // returns 'HealthBar (UnityEngine.UI.Image)'
    24.     }
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         showBar();
    29.         bossbarHealth.fillAmount = (float)c_enemy.currentHealth / c_enemy.maxHealth;
    30.         Debug.Log(bossbarHealth.fillAmount);
    31.     }
    32.     void showBar()
    33.     {
    34.         if (!activeBar)
    35.         {
    36.             activeBar = true;
    37.             // TODO: corrigir a posição de instanciação
    38.             GameObject instBossBar = Instantiate(bossBar, transform.position, Quaternion.identity);
    39.             instBossBar.transform.SetParent(UI.transform);
    40.  
    41.             bossbarName = FindObjectOfType<Text>();
    42.             bossbarName.text = bossName;
    43.             if (c_enemy.currentHealth <= 0)
    44.             {
    45.                 // TODO: não tá deletando quando o inimigo morre
    46.                 Destroy(instBossBar);//, 1f);
    47.             }
    48.         }
    49.  
    50.     }
    51. }
     
  2. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    is your UI image type set to Filled?
    Filled Image type values are between 0 and 1. do currentHealth and maxHealth comply with that?

    also it doesn't destroy because showBar() only runs once because of !activeBar statement.
    It's set to true right after it instantiates the UI therefore can not check "c_enemy.currentHealth <= 0" statement.


    If you would ask me, I would have done that like this,

    One slider to rule them all.
    You can change its values without instantiate and destroying,
    you can just set its alpha to 0 when the boss is dead.

    Spawn a boss, set its maxValue to bosses health,
    kill a boss, set its alpha to 0,

    rinse and repeat
     
    Dounat12 likes this.
  3. SrAranha

    SrAranha

    Joined:
    Jan 26, 2022
    Posts:
    5
    Yes it is set to Filled, i can even change the fillAmount in inspector, and currentHealth and Max Health do comply with it, i've setup a debuglog before to check this and the fillAmount in Update is between 0 and 1.
    Do you know if there is a way to make it instantiate only once? Because by now it keeps creating new bossbars.

    And i've done what you sugested first, it works perfectly, but i was trying another way to do it, and maybe i'll return to it in the end.
     
  4. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    are you sure you are settign the correct value?
    i see you are updating bossbarHealth.fillAmount in Update method
    but you instantiate " instBossBar" and not changing its value.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    It looks like you're setting the fill amount on the prefab rather than on the actual instantiated object in the scene.
     
  6. SrAranha

    SrAranha

    Joined:
    Jan 26, 2022
    Posts:
    5
    Yep i'm sure the value is correct, and as you and PreatorBlue said, i was indeed changing the prefab not the intantiated object, i've changed to it and now works, the problem is that it keeps creating new bossbars, still don't know how i can do it once.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    Just create the bar once in Start and activate/deactivate it as needed.
     
  8. SrAranha

    SrAranha

    Joined:
    Jan 26, 2022
    Posts:
    5
    But then i can't access it from outside Start. Sorry if i'm being dumb, just new to unity at all.
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,932
    Why not? That's what variables are for...

    Code (CSharp):
    1. GameObject prefab;
    2. Image instance;
    3.  
    4. void Start() {
    5.   instance = Instantiate(prefab).GetComponent<Image>();
    6. }
    7.  
    8. void SomeOtherMethod() {
    9.   instance.fillAmount = 5;
    10. }
     
  10. SrAranha

    SrAranha

    Joined:
    Jan 26, 2022
    Posts:
    5
    So, i did what you sent, and by some reason it didn't work until i restarted unity