Hi guys i have a problem . i asked the same question few days ago but that didn’t work…
my problem was i have created a button and health text …when ever i press the button health decreases by 1…but when i play the game it only happen once not multiple times…here is my code …bye the way i m a new in coding so don’t make fun of me (just kidding u can)…
public int health = 100;
public GameObject Textboxhealth;
public void Button() {
int nhealth = MHealth ();
Textboxhealth.GetComponent<Text> ().text = "" + nhealth;
}
public int MHealth() {
int nhealth = health - 1;
return nhealth;
}
using UnityEngine.UI;
public int health = 100;
//In case you want to save the original health
private int _health;
public Text Textboxhealth;
void Start()
{
_health = health;
}
public void RemoveHealth()
{
// -= Will tell the int to subtract however many from itself
health -= 1;
// Then update text
Textboxhealth.text = health + "";
}

Put the game object in the OnClick() area (below runtime only) as circled, then on the drop down near “Runtime Only”, find your script, then scroll through the area to find RemoveHealth(). This will then have your button run this function everytime it is selected. To use your Text, just find the text child of button and attach it to the script in the inspector
Hello,
the code you’re using has some problems, but don’t worry, and if someone laughed at you just ignore them, we all have been there.
OK,
Even though i think it’s not a good way to make a health system like this, but I’ll show you just the way you want to implement it
Here’s the code, with comments on each part, that shows you what that part is doing.
using UnityEngine;
using UnityEngine.UI;
public class Tes : MonoBehaviour
{
//this is a field that contains the health, since it has [SerializeField] on it, it can be
//initialized from the editor
[SerializeField] private int _health;
//codes in this method Start() only one when the game starts.
private void Start()
{
//this line says if health didn't get initialized from the editor and it is 0 when the game starts
//, set it to 100, other wise this line of code will be ignored.
if (_health == 0)
{
_health = 100;
}
}
//codes of this method Update() runs once every frame
//for example if you play the game at 60 fps, this method runs 60 times per second.
private void Update()
{
//if health is greater than 0, basically if the player/enemy is alive, run the Button() method.
if (_health > 0)
{
Button();
}
}
private void Button()
{
//if space key is pressed, decrease the health by 1
//and convert it to string then set it to the Text component of this game object.
if (Input.GetKeyDown(KeyCode.Space))
{
_health--;
GetComponent<Text>().text = _health.ToString();
}
}
}
i made a game object and added a text component and this script to it.
because i see you added a text component on your Textboxhealth.
Hope i helped.
You never change the value of your var health , so everytime you press the button you get 99.
public void Button() {
health = MHealth ();
Textboxhealth.text = health.ToStrig();
}
You have to declare Textboshealth as public Text Textboxhealth;
instead of public GameObject Textboxhealth;
Other examples are good, but this is something I would use as you can expand upon it and use some of the stuff here with other scripts that would typically need to know health status and stuff:
public class PlayerHealth : Monobehaviour
{
int currentHealth;
public int maxHealth;
public int damage;
public bool dead;
//Make sure you set maxHealth in the inspector
private void Start()
{
currentHealth = maxHealth;
dead = false;
}
//Make sure you set damage amount in the inspector
public void ButtonClick()
{
if(currentHealth > 0)
{
currentHealth -= damage;
}
if(currentHealth <= 0)
{
currentHealth = 0;
dead = true;
//Here you can either call a revive function or just reset health back to maxHealth right here.
Revive();
}
}
private void Revive()
{
if(dead = false)
{
return;
}
else
{
currentHealth = maxHealth;
dead = false;
}
}
}
On your UI, attach this script to the button and have the ButtonClick() function call to degrade your health to whatever you set the damage as.