- Home /
How do I change text in a Text Mesh Pro GUI attached to my prefab
I have a prefab Game Object with a child canvas, that has a child Text mesh pro. I am using a an input field that assigns a name string to the prefab and I want the text mesh pro to display that name. When I run the program the prefabs are storing the string correctly but the text is only changing for the first instantiated prefab.
public class Draw_Circle : MonoBehaviour { public Color fillColor = Color.blue;
public Slider radiusAdjuster;
private MeshFilter _meshFilter;
private LineRenderer _lineRenderer;
private CircleCollider2D _circleCollider2D;
public float radius;
public void Awake()
{
_meshFilter = GetComponent<MeshFilter>();
_lineRenderer = GetComponent<LineRenderer>();
_circleCollider2D = GetComponent<CircleCollider2D>();
}
private void Update()
{
Create();
print(this.gameObject.name);
GameObject.Find("Canvas/Test").GetComponent<TextMeshProUGUI>().SetText(this.gameObject.name);
public class TaskCreation : MonoBehaviour { public Toggle EnableSwitch; public TMP_InputField inputField;
public GameObject Entity;
public GameObject Panel;
public string nameText;
public void OpenPanel()
{
if(Panel != null)
{
bool isActive = Panel.activeSelf;
Panel.SetActive(!isActive);
if(isActive == true && EnableSwitch.isOn)
{
Produce();
}
}
}
public void NameAssign(string newName)
{
nameText = newName;
}
public void Produce()
{
float h = 2f * Camera.main.orthographicSize;
float w = h * Camera.main.aspect;
float x0 = 0 - w / 2;
float y0 = 0 - h / 2;
float x1 = x0 + w;
float y1 = y0 + h;
Vector3 position = new Vector3(Random.Range(x0, x1), Random.Range(y0, y1), 0);
print(nameText);
Entity.name = nameText;
Instantiate(Entity, position, Quaternion.identity);
Debug.Log(Entity.name);
//GameObject.Find("Canvas/Test").GetComponent<TextMeshProUGUI>().SetText(Entity.name);
}
}
Your answer
Follow this Question
Related Questions
UI components all disappear on Play mode, then stay invisible 2 Answers
Sphere and text mesh in prefab share transformation matrix 0 Answers
Setting a value to a textPrefab 1 Answer
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