how to connect my Health to my UI
ok so im new to C# and needing help i want to send my health information to my ui text..and not sure were to start here is my code
public class TestHealth : MonoBehaviour{
public int Health = 100;
void OnTriggerEnter(Collider col)
{
// set at 4 hits = a kill
if ((col.gameObject.tag == "AstroidTag"))
{
Health = Health - 25;
Destroy(col.gameObject);
}
// set to 3 hits = a KILL
if ((col.gameObject.tag == "Bullet"))
{
Health = Health - 35;
}
// Health Pack = 100
if ((col.gameObject.tag == "Health"))
{
Health = Health = 100;
}
if (Health <= 1)
Destroy(gameObject);
if (Health >= 100)
Health = 100;
}
}
Answer by lvskiprof · Sep 17, 2021 at 06:51 PM
First, it would help if we could see what you are using to display the Health here. You did not include the definition for that.
Second, why are you destroying the asteroid GameObject (Destroy(col.gameObject);), but not the bullet? Generally, once a bullet hits something it is considered destroyed.
Third & Fourth, how is it that you are seeing collisions on a GameObject with a tag of "Health"? I wouldn't expect that to have a Collider attached to it. Also, you are simply setting it twice to a value of 100 there. Did you mean: Health = Health + 100?
At the end of all this you can update the displayed value for Health, or you can create a method that does it and call that each place where you are changing the value of Health. The latter solution would mean you don't update it if Health is not changing and that method would update the displayed value.
You might also want to set Health to 0 when it is <= 1, just as you don't let it become greater then 100 you might want to set a lower limit of 0.
Your answer
Follow this Question
Related Questions
UI Text not displaying public int but will display private int 1 Answer
Score UI per player 1 Answer
Memory problem on android/ios 1GB ram with string[]? 0 Answers
Health system in a text based game. 2 Answers
Text doesn't change with string C# 1 Answer