Hello,
I did a binding with a Boss health bar and a UI Elements yesterday. It was working yesterday, I didn’t change anything and today it’s not working.
Script on my Game object in game “BossHealth.cs”:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class BossHealth : MonoBehaviour
{
public int maxHealth;
public int currentHealth;
public float percentHealth;
private void Start()
{
maxHealth = 3000;
currentHealth = 2000;
}
private void Update()
{
percentHealth = ((float)currentHealth / maxHealth) * 100;
}
}
Script for my UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using System;
public class bind_boss_bar : MonoBehaviour
{
private VisualElement m_Root;
private Label m_BossName;
private ProgressBar m_BossLife;
private VisualElement m_BossVisualElement;
public GameObject bossScript;
public float bossCurrentHealth;
public float bossMaxHealth;
public float bossPercentHealth;
void OnEnable()
{
m_Root = GetComponent<UIDocument>().rootVisualElement;
m_BossName = m_Root.Q<Label>("raid-boss-name");
m_BossLife = m_Root.Q<ProgressBar>("raid-boss-life");
m_BossVisualElement = m_Root.Q<VisualElement>("raid-boss-progress-bar");
//Access game object and get value
bossScript = GameObject.Find("Boss");
bossCurrentHealth = bossScript.GetComponent<BossHealth>().currentHealth;
bossMaxHealth = bossScript.GetComponent<BossHealth>().maxHealth;
//Update progress bar
m_BossLife.value = ((float)bossCurrentHealth / bossMaxHealth)*100;
SerializedObject so = new SerializedObject(bossScript.GetComponent<BossHealth>());
m_Root.Bind(so);
m_BossLife.bindingPath = "bossPercentHealth";
}
}
And in my UI Elements I set :
Binding path : “bossPercentHealth”
It only works if I add this code on my script for my UI , but it’s not a binding at all…
private void Update()
{
bossCurrentHealth = bossScript.GetComponent<BossHealth>().currentHealth;
bossMaxHealth = bossScript.GetComponent<BossHealth>().maxHealth;
m_BossLife.value = ((float)bossCurrentHealth / bossMaxHealth)*100;
}