How to get the component of an instantiated prefab?

I would like to set the text of instantiated prefab.Inside the prefab I have added the TextMesh Pro component.How to get the textmeshpro inside the instantiated prefab.I am attaching the pic of arrangement of the Textmeshpro inside the instantiated prefab.Here I think I want to get the text component inside the Womansuit(Clone)>Canvas>TextMeshPro - InputField>TextArea>Text .How can I achieve this?

public TMP_Text tm1;

//Trying to get the component below.Testing the code but getting null pointer exception
UnityARHitTestExample.HittestInstance.m_HitTransform.transform.GetChild(1).transform.GetChild(1).transform.GetChild(1).gameObject.GetComponent<TMP_Text>() = "Hello";

The first child of a transform is index 0, not index 1.

If you’re not generating dozens or hundreds of these per frame, and there is only one TMP_Text to worry about in the prefab, I would just go ahead and use the spawnedObject.GetComponentInChildren<TMP_Text>().

If there are multiple TMP_Text inside the prefab, you can decide to do GetComponentsInChildren (plural) instead, or you can make a separate script at the top level which will always be pre-setup with the specific references you need. For example, on WomansSuit prefab’s top object, have a new script called Labeled and in that script have a field called TMP_Text theLabel. Then you can more likely trust the spawnedObject.GetComponent().theLabel is what you want to manipulate after spawning.

Sure, if you’re trying to squeeze every last erg out of performance the GetComponentInChildren approach is a little lazy, but during development you may rearrange what is going on inside the prefab, so this won’t need constant updating.

1 Like
 public TextMeshPro tm1;

tm1 =   UnityARHitTestExample.HittestInstance.m_HitTransform.GetComponentInChildren<TextMeshPro>();
 tm1.SetText ( messagss);

Still getting null pointer exception…Is it the way to set text Textmesh pro

What is null here? Try to Debug.Log out these: UnityARHitTestExample.HittestInstance.m_HitTransform and find out what is null/ It looks like it is some kind of static variable? Why? You told it’s an instantiated prefab.
Where you instantiate the prefab you can grab the instance, examples here:
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Infield1=womansuit.GetComponentInChildren<TMP_InputField>();

TMP_InputField …it was not textfield :)…yes if there is only one component we can find by GetComponentInChildren…

 Infield11 = womaninsuit.GetComponentInChildren<TMP_InputField>();
            Infield11.text = messagss;
            Debug.Log("Componet text..." + Infield11.text);

Here I am getting the text = Componet text…“How are you”?.But that does not get updated on the Inputfield .When I run again the Input fiels is having previous values.