unity | Find child gameObject with specified parent

17,904

Solution 1

Your question is a little confusing. This is what I understand from what you are asking. How to find a child object of parent object that changes its name during run time.

You can do this with the FindChild function but there is another way of doing this. You can do it with just the Find function.

By using the Find function, when you use '/' before the name of the gameobject you are looking for, Unity takes it like you are looking for a child GameObject.

For example, GameObject.Find("state") will look for parent GameObject called state.

If you use GameObject.Find(/Country/state), Unity will ONLY search for a GameObject named "state" that is inside a parent GameObject called "Country".

So assuming that your parent GameObject is called Country and your child GameObject is called State, you can find the child of the Country GameObject with

GameObject.Find("/Country/State");

Or like this

string nameOfParentObject = "ParentObject";
string nameOfChildObject = "ChildObject";

string childLocation = "/" + nameOfParentObject + "/" + nameOfChildObject;
GameObject childObject = GameObject.Find (childLocation);

You can even put this into a fucntion if you need to search for child GameObject from a parent more often

GameObject findChildFromParent (string parentName, string childNameToFind)
{
    string childLocation = "/" + parentName + "/" + childNameToFind;
    GameObject childObject = GameObject.Find (childLocation);
    return childObject;
}

Now, anytime you want to search for a GameObject child inside a parent GameObject, you can just call the function which will return the GameObject.

Let's just say that the name of your parent class is Jinbom but then changed to "Country" during runtime while your child object name remains as "State". You can find the "State" GameObject by doing this

GameObject childObject = findChildFromParent ("Country", "State");

And to get the name of your GameObject that changed during runtime, you can just use yourgameObject.name.

You can do this during runtime

GameObject childObject = findChildFromParent (yourgameObject.name, "State");

Solution 2

the code can be more effective if you modify like this :

GameObject stateGo = go.transform.FindChild("State").gameObject;
stateGo.name = userIndex.ToString();
stateGo.SetActive(false);

And I think you need to elaborate more about your question.

Share:
17,904
홍의숙
Author by

홍의숙

Updated on June 04, 2022

Comments

  • 홍의숙
    홍의숙 almost 2 years

    I have to find child gameObject with specified parent. Uhm. Let me explain more detail. I'm creating the prefab which have one child gameObject named 'state' and the name of parent gameObject is changed when i play the game. I mean the name of child gameObject is always same but the name of parent gameObject is all different. How can i know which child one is under specified parent one. It's not easy as i thought. This is the code below that i've tried.

    go = Instantiate(Resources.Load("Prefabs/User")) as GameObject;
    go.transform.FindChild("State").gameObject.name = userIndex.ToString();
    go.transform.FindChild(userIndex.ToString()).gameObject.SetActive(false);
    

    This code is not effective because the name "state" is equal to all. How can i modified more clear and effective. Please give me some idea.


    For example, the created name of four prefabs are 'a','b','c', and 'd'. All gameObjects have same name of child ;'state'. All I want to know is how to find the 'state'(child gameObject) which is under 'a' (parent gameObject). I don't know much about the unity feature. How can i deal with finding the child under desired parent.