- Home /
Edit textmesh of prefab child object after instantiate of parent during a loop C#.
Hi Guys - no idea what i'm doing.. please help.
I'm failing to edit textmesh of prefab child object after instantiate of parent during a loop.
project setup---> Prefab Parent Object name = GCube Prefab Child Object = GCubeText (relationship formed by dragging together in Hierarchy then dragging to project)
project code (component of empty game object) --->
//starting loop
for (int i = 0; i < pr.Rows.Count; i++){
Instantiate(prefab, spawnlocation[i], Quaternion.identity);
//renaming prefab object to row number
prefab.name = i.ToString();
//edit childcomponent textmesh
var prefabchildtext = GameObject.Find("GCubeText").GetComponent(typeof(TextMesh)) as TextMesh;
prefabchildtext.text = "please help me";
any of that make sense:)
Currently everything spawns correctly (if I remove the textmesh edit lines), names of prefabs change correct (accept can't get rid of the clone), but the 3dtext attached to the cubes all still say the default "hello world".
with these textmesh edit lines i only get one cube instead of the full loop..
Cheers
Did you ever figure this out, because I am having a similar problem. I am finding the prefab no problem, but when I try to transform to the child it is not finding it. "NullReferenceException: Object reference not set to an instance of an object"
Answer by artrynk81 · Apr 08, 2013 at 06:16 AM
I don't see where you have access to your instantiated prefabs
for (int i = 0; i < pr.Rows.Count; i++){ }
prefab.name = i.ToString(); -> This give something like "0","1","2","3".... prefab names in scene
but after that you try access to prefab this maner:
var prefabchildtext = GameObject.Find("GCubeText").GetComponent(typeof(TextMesh)) as TextMesh;
But in fact your instantiated prefabs name != "GCubeText"
but the 3dtext attached to the cubes all still say the default "hello world
right, your GameObject.Find dont find GCubeText in the scene you must search for "0","1","2"....
Your answer
Follow this Question
Related Questions
Prefab instantiation: am I missing something? 1 Answer
Instantiate prefab once help C# 1 Answer
How do I Instantiate a prefab in a list? 0 Answers
OverlapSphere for parallel arrays 1 Answer
Placing a road 0 Answers