My canvas has a healthbar with a script that controls the slider value (Healthbar.cs) The Event System is in place, the slider script has been dragged into the public function.
My player object has a “stats” script, which defines float values like “health”, “healthMax”, “ApplyDamage” and so on. Running next to that is “Bar_Control”, which (in theory) references those values in the “stats” script and passes them on to the healthbar as slider values. (yes I’ve dragged the healthbar object into the public variable. Still no dice. Thoughts?
Healthbar:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public Slider slider;
public void SetMaxHealth(float health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(float health)
{
slider.value = health;
}
}
Bar_Control:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bar_Control : MonoBehaviour
{
float health;
float healthMax;
public Modified_Player_Stats statsScript;
public HealthBar healthBar;
public void Start()
{
statsScript = GetComponent<Modified_Player_Stats>();
health = statsScript.health;
healthMax = statsScript.healthMax;
healthBar.SetMaxHealth(healthMax);
}
public void Update()
{
healthBar.SetHealth(health);
}
}
And you’ve dragged the actual slider on to your HealthBar script component Slider field? (I think you get a null object error if not, so you must have).
If you put a debug log into SetMaxHealth does it print anything? We need to know if that function is actually getting called. Do the same for SetHealth
You need to find out where the problem is: SetMaxHealth and SetHealth are never getting called? Or SetMaxHealth is only called from the Start of Bar_Control:
For one issue though. This line of code in the Start of Bar_Control
health = statsScript.health;
only sets the value of health at Start. After that, health is never updated again. Because, if health is a float, you have just made a copy of the player health. It is not a reference to the player health variable.
If you want to make sure player health is the current health in Update, you must include that code there too.
healthBar.SetHealth(statsScript.health);
C# fundamental variable types are never passed by reference. if you state:
int a = 1;
int b = a;
b++;
We have just made a copy of the value of a. After this code executes a= 1 and b= 2. It doesn’t matter if you are referencing such variables in other scripts or not. You are always only copying the value of such variables.
So, unless you are processing health in some way in Bar_Control, as it is now you don’t even need that variable. Just access statsScript.health directly.
@streeetwalker Thank you for your input! It worked! For reference, this is Bar_Control now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bar_Control : MonoBehaviour
{
public Modified_Player_Stats statsScript;
public HealthBar healthBar;
public void Start()
{
statsScript = GetComponent<Modified_Player_Stats>();
healthBar.SetHealth(statsScript.health);
healthBar.SetMaxHealth(statsScript.healthMax);
}
public void Update()
{
healthBar.SetHealth(statsScript.health);
}
}