- Home /
Transform.Find failing to find named child in clone
In my code I create a UI element which is a hierarchy of objects made into a prefab. I then instantiate at runtime. I then want to find child objects by name so that I can modify them in the script. When I call Transform.Find it fails to find the object. I wrote a function that builds a dictionary of the children by name, and that works. I don't understand why the built in function is not working for me.
GameObject clone = (GameObject) Instantiate(levelFab);
// Transform text = clone.transform.Find("LevelName");
FindChildren(clone.transform, d);
private void FindChildren(Transform transform, Dictionary<String, GameObject> d)
{
foreach(Transform t in transform)
{
d.Add(t.name, t.gameObject);
Debug.Log("adding " + t.name);
FindChildren(t, d);
}
}
http://answers.unity3d.com/questions/689549/find-child-objects-by-name.html
I suspect maybe you are calling this function from a script on a different object than the children object's parent. If that is the case, you are searching all the children of the object calling this function. Try changing the term "transform" to something else and see if that works.
private void FindChildren(Transform transform, ... etc
to
private void FindChildren(Transform parentTrans, ... etc
//...
foreach(Transform t in parentTrans)
//...
$$anonymous$$y function is working though. It is the built in one that is not, and they both are called on the same object.
Answer by jenci1990 · Nov 30, 2014 at 10:43 PM
Use transform.FindChild("ChildName");
Transform test = clone.transform.FindChild("LevelName");
returned null
You can't find children's child. You have to know the hierarchy. example: my first child is "child1" and this child is "child2". You can find it by this:
Transform test = transform.FindChild("child1/child2");
but
transform.FindChild("child2");
not work!
Ah, I understand. Thanks for the explanation jenci1990. I shall stick with my function :)
Your answer

Follow this Question
Related Questions
transform.Find(string)? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# The call is ambiguous between the following methods or properties 1 Answer
Snap Back? 1 Answer