- Home /
Making a 3d text object represent the "health" of an object?
Hello there, relatively new to unity and c# but right now i'm having a bit of an issue
I have this 3d text object on a "door" object, and said door will slide upward to open when the button right next to it is shot enough times. I want the 3d text's "Text Mesh" to be the remaining "health" value of the button. How is this done? I tried this--obviously i couldn't get it to work, i just don't know why.
here is the script on the button, containing the "health" public class shootbutton : MonoBehaviour {
public static float health = 600;
public float bulletdamage = 1;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "bullet")
{
health = health - bulletdamage;
}
}
}
and here is the nonfunctioning script for my 3d text object
public class scorereport : MonoBehaviour {
void Update () {
GetComponent.TextMesh().text = FindObjectOfType<bulletbehavior>().health;
}
}
Answer by W01Fi3 · Jan 13, 2018 at 07:38 AM
So here's a simple example script to tackle this
JavaScript Version:
//The TextMesh component of the text, assign this in the inspector.
var TheText : TextMesh;
//The hitpoints of the button or whatever
var Health : int;
//When it gets shot
function Bang()
{
//Subtract health by 1
Health -= 1;
//Sets the text to Hitpoints: <A number>
TheText.text = ("Hitpoints: " + Health);
}
C# Version:
//The TextMesh component of the text, assign this in the inspector.
public TextMesh TheText;
//The hitpoints of the button or whatever
public int Health;
//When it gets shot
void Bang()
{
//Subtract health by 1
Health -= 1;
//Sets the text to Hitpoints: <A number>
TheText.text = ("Hitpoints: " + Health);
}
so there is something i must be missing!!
First of all, thank you so much you've set me on the right track. Second, do i put this script on the 3d text or the button?
and if it is one or the other, are there scripts i need to add to the other one?
should i put a boolean in so that something can activate "Bang?"
Sorry for the delay, idk if you still need help but I'll try.
So first of all, I would suggest you put the script on the button, since you already have a reference to the text (The variable named TheText)
Next question, no, you only need one script, the one on the button.
Finally, I'm not quite sure what you mean, but method "Bang" was just an example, in your case, you can replace "Bang" with the
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "bullet")
{
ect...
If you have any more questions or need it explained more thoroughly, just ask or contact me on discord if you have it. $$anonymous$$y discord is W01Fi3#8200
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting variables by their names? 3 Answers
get component javascript from a c# script 3 Answers
Accessing Javascript to C# script 1 Answer