Why does GetComponent().SetText only change the Text mesh component of the first Prefab?
In my project I have a window that contains an input field. The script for the window then spawns a prefab named whatever I input. The script that I have running in the prefab that is instantiated has a function that should change the text mesh into this new name. However, when i run this script the name changes for the first instantiated prefab and all further prefabs have a the default name I typed into the text mesh component.
My prefab has a child canvas that contains the child text mesh pro UGUI
This is the Code I use for the window
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); //this is to make sure the name is being taken in
Entity.name = nameText; //this is to assure the entity is renamed
Instantiate(Entity, position, Quaternion.identity);
Debug.Log(Entity.name); // this is a second check
}
This is the code that I run within my prefab
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612232748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
TMP text that was changed from script is overridden by the one in editor 0 Answers
Player Prefab scale automatically goes to 1 x 1 x 1 whereas I have set it to 0.48 x 0.48 x 0.48 1 Answer
Script updates prefab text 0 Answers
Lag When Instantiating Particle System Prefab On Collision 0 Answers
How can I modify/change a prefab? 1 Answer