using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBarController : MonoBehaviour {
basically I just need to be able to reduce health values some how. When the player touches an object like a mob. The script is attached to the slider itself. I think I need to reference the player some how in the script but I am not sure how. New to C# and most code generally. I have been up for 36 hours so I am sorry if my spelling is out of wack or whatever I just want to fix my game god damn it ha. Thank you.
First thing, you should use code tags when your pasting code, just so its easier to read:
I would keep the health value inside the script of the entity that has the health, rather than inside the script that controls the slider. This isn’t super important, but just as a way to keep “slider bar” stuff and its logic, away from “character stuff”.
So when your player runs into an enemy, you would probably have the enemy do some kind of attack, or perhaps you would have a trigger that notices the player touched the enemy. When one of those things happens, you would have a reference to the player, and call a method like “HurtPlayer()” on the playerscript.
Then you could have the player script reduce the players health by some amount based on how tough the enemy is or something, and in turn, the player script would notify the health bar script that the player health has changed. This could be as simple as having the player script keep a reference to the health bar script and change a public value for how full the bar is, or you could have the bar script check the players health variable, and change when the value changes.
Perhaps if the things I’m saying are not making sense, you could spend some time doing a few tutorials to get some stuff figured out. I’d start in the learn section ( Learn ) and just make sure you understand all the basics there. Once you have that done, re-read my suggestions and let me know specifically what area you need help with.
You basically mentioned the solution already, that’s also what has been recommended by @MD_Reptile with a different wording.
Instead of the integer field for the health value, you can use the script component type that you use to manage your health. If that’s baked into your player script, you use your player script. But it could also just be a seperate health-only component (depends on how much you seperate the concerns).
Then, instead of setting the slider’s value to the currentHealth (which has a typo in your script, btw), you’ll access that referenced component and get the actual health value.
Even better would be some sort of notification, as the slider only needs to change when the health value has changed, i.e. your slider-update component doesn’t need to run update itself. But that’s probably a concept (event-based) that you want to look at later.
I agree though, that you might wanna practive a little more with some tutorials to get a better understanding of how things work together in general, and in Unity.