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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BossBar : MonoBehaviour
{
public string bossName;
public GameObject bossBar;
public GameObject UI;
bool activeBar = false;
EnemyController c_enemy;
Text bossbarName;
// TODO: pegar a imagem correta
Image bossbarHealth;
// Start is called before the first frame update
void Start()
{
c_enemy = GetComponent<EnemyController>();
bossbarHealth = bossBar.GetComponentInChildren<Image>();
Debug.Log(bossbarHealth); // returns 'HealthBar (UnityEngine.UI.Image)'
}
// Update is called once per frame
void Update()
{
showBar();
bossbarHealth.fillAmount = (float)c_enemy.currentHealth / c_enemy.maxHealth;
Debug.Log(bossbarHealth.fillAmount);
}
void showBar()
{
if (!activeBar)
{
activeBar = true;
// TODO: corrigir a posição de instanciação
GameObject instBossBar = Instantiate(bossBar, transform.position, Quaternion.identity);
instBossBar.transform.SetParent(UI.transform);
bossbarName = FindObjectOfType<Text>();
bossbarName.text = bossName;
if (c_enemy.currentHealth <= 0)
{
// TODO: não tá deletando quando o inimigo morre
Destroy(instBossBar);//, 1f);
}
}
}
}
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,
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.
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.
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.