unity3d - unity | Find child gameObject with specified parent -
i have find child gameobject specified parent. uhm. let me explain more detail. i'm creating prefab have 1 child gameobject named 'state' , name of parent gameobject changed when play game. mean name of child gameobject same name of parent gameobject different. how can know child 1 under specified parent one. it's not easy thought. code below i've tried.
go = instantiate(resources.load("prefabs/user")) gameobject; go.transform.findchild("state").gameobject.name = userindex.tostring(); go.transform.findchild(userindex.tostring()).gameobject.setactive(false);
this code not effective because name "state" equal all. how can modified more clear , effective. please give me idea.
for example, created name of 4 prefabs 'a','b','c', , 'd'. gameobjects have same name of child ;'state'. want know how find 'state'(child gameobject) under 'a' (parent gameobject). don't know unity feature. how can deal finding child under desired parent.
your question little confusing. understand asking. how find child object of parent object changes name during run time.
you can findchild function there way of doing this. can find function.
by using find function, when use '/' before name of gameobject looking for, unity takes looking child gameobject.
for example, gameobject.find("state")
parent gameobject called state.
if use gameobject.find(/country/state), unity only search gameobject named "state" inside parent gameobject called "country".
so assuming parent gameobject called country , child gameobject called state, can find child of country gameobject with
gameobject.find("/country/state");
or
string nameofparentobject = "parentobject"; string nameofchildobject = "childobject"; string childlocation = "/" + nameofparentobject + "/" + nameofchildobject; gameobject childobject = gameobject.find (childlocation);
you can put fucntion if need search child gameobject parent more often
gameobject findchildfromparent (string parentname, string childnametofind) { string childlocation = "/" + parentname + "/" + childnametofind; gameobject childobject = gameobject.find (childlocation); return childobject; }
now, anytime want search gameobject child inside parent gameobject, can call function return gameobject.
let's name of parent class jinbom changed "country" during runtime while child object name remains "state". can find "state" gameobject doing
gameobject childobject = findchildfromparent ("country", "state");
and name of gameobject changed during runtime, can use yourgameobject.name
.
you can during runtime
gameobject childobject = findchildfromparent (yourgameobject.name, "state");
Comments
Post a Comment