- Home /
Setting a value to a textPrefab
Help guys. I have a prefab with a TextMesh component attached to it as well as a script to alter the text. when an enemy is killed, the prefab is instantiated showing hit points. But i have no idea how to make certain enemies have different points for the textprefab. I tried setting a value on each enemy which will then be attributed to the script on the textPrefab instantiated by the enemy, but it is all a mess.
Answer by lcplrice · May 21, 2020 at 05:50 AM
I'm new also but ill give this a shot since no one else has said anything yet. You may already have tried the code below, maybe post some sample code of what you have tried already if this idea doesn't help.
Prefab Script:
public class PrefabScript : MonoBehaviour
{
public void ShowHitPoints(int displayValue)
{
GetComponent<TextMesh>().text = displayValue.ToString();
}
}
Enemy:
...
public GameObject textPrefab;
protected int hitPoints = 5;
public void Die()
{
GameObject go = Instantiate(textPrefab);
go.GetComponent<PrefabScript>().ShowHitPoints(hitPoints);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612234733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I change text in a Text Mesh Pro GUI attached to my prefab 0 Answers
How to generate prefab with random number inside it ? 1 Answer
textmesh in prefab folder updating but prefabed textmesh in scene isn't updating in scene 0 Answers
Edit textmesh of prefab child object after instantiate of parent during a loop C#. 1 Answer
UI components all disappear on Play mode, then stay invisible 2 Answers