My healthbar and player won't talk to each other! Do we need counseling?
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);
}
}
Answer by streeetwalker · Mar 11, 2020 at 12:08 PM
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.
Answer by CerebralStatic · Mar 13, 2020 at 04:53 AM
@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);
}
}
@CerebralStatic , glad to hear it worked. Hey, I got this message about your question with respect to not needing Bar_Control at all, and you getting null reference errors. Sorry for the late reply, but I can't seem to find that question/comment referenced in the email notification. Did you solve that problem?
Oh yeah, I tried out multiple solutions and one of them involved tossing out the Bar_Control script altogether. But shortly after that didn't work and I posted the question, I followed your original advice more closely and found that it worked perfectly, thus answering my other question. So ins$$anonymous$$d of leaving the unnecessary question, I changed it into a "Thanks this worked!" response. BTW, more importantly than having a solution, I'm grateful that I now understand what was wrong, what the problem means, what the solution means, and now I can use that knowledge to better understand this space and coding in general.