- Home /
GameObject.Find, but only inside of a specific object
I'm sure this has been answwered somewhere, but I couldn't find it. Basically, I went to find all GameObjects named "food" INSIDE of a specific GameObject. Since gO.Find() defaults to the ENTIRE scene, how can I say something like:
GameObject.Find("food" inside of AREA ONE)
Answer by vexe · Sep 17, 2013 at 03:54 AM
Just use GetComponentsInChildren, it's super easy to use, it's recursive, it's cool:
// This will get the `Transform` component from all of `areaOne` children,
// and all children of its children, and all children of its children children,
// and all..., well, you get the idea...
var children = areaOne.GetComponentsInChildren<Transform>();
foreach (var child in children)
if (child.name == whatYoureLookingFor)
// do something
Also,
http://docs.unity3d.com/Documentation/ScriptReference/Transform.IsChildOf.html
can be useful and elegant.
IsChildOf means child or deep child (recursive child if you will).
So you can simply say ... get everything named "Food" and then in each case simply check if it is in the "area" you are after, using IsChildOf.
Note too that you may want to use tags or layers for this type of problem. Hope it helps.
you could be looking for something this ...
function FindUnderHereNamed(name:String):Transform // (finds one only)
{
var transforms = transform.GetComponentsInChildren(Transform);
for (var t : Transform in transforms)
{
if (t.name == name) return t;
}
return null;
}
it's an excellent point. there are so many slightly different ways to get/find things/children, it's hard to know or remember which is best in a given situation! :)
This is the thing with Unity, so many ways to do things. The best benefit comes when you try and understand all those different ways, that's when you start to UNDERSTAND things... (which is why I showed multiple ways in my previous answer) But once you do experiment with all those possibilities, you would then settle on the easiest, cleanest and most efficient one you came across. Which is why I removed my old answer...
lol vexe I just realised this is the same as my "standard routine" (above) I use for this problem! heh
Answer by DeveshPandey · Sep 11, 2013 at 07:52 AM
If you know the name of parent game object then you can use -
GameObject.Find("ParentGameObjectName/food");
Answer by glantucan · Sep 10, 2014 at 10:38 AM
How about this?
GameObject childrenGO = parentGO.transform.Find("ChildGOName").gameObject;
It's way shorter and I'd guess more performant.
note, "FindChild" is nowadays "Find"
You may have to GameObject.Find("parentGOName") first if you don't have a reference to it already. I use this when I have a lot of instances of the same prefab with the same name on the scene and want to find something inside them.
Answer by Max_power1965 · Jun 16, 2016 at 09:26 AM
I've created a simple extension method that allow you to search a component only inside of the selected gameObject:
public static GameObject FindObjectInChilds(this GameObject gameObject, string gameObjectName)
{
Transform[] children = gameObject.GetComponentsInChildren<Transform>(true);
foreach (Transform item in children) {
if (item.name == gameObjectName) {
return item.gameObject;
}
}
return null;
}
Usage:
this.gameObject.FindObjectInChilds("You object name");
yup that worked. Thanks a lot.
Just want to add some more :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Constants {
public static GameObject FindObjectInChilds(this GameObject gameObject, string gameObjectName)
{
Transform[] children = gameObject.GetComponentsInChildren<Transform>(true);
foreach (Transform item in children) {
if (item.name == gameObjectName) {
return item.gameObject;
}
}
return null;
}
}
AND THEN CALL it like
Constants.FindObjectInChilds(transform.gameObject,"Bip001 R Forearm").transform;
Thank you so much hammad-zahid your code is useful :3
Your answer
Follow this Question
Related Questions
Find a GameObject exactly in position Vector3(1,2,3)? 3 Answers
Searching a List of GameObjects by name 2 Answers
Unity 2d static variables versus GameObject.Find("") 2 Answers
Find right mesh for collider 1 Answer
Insert Transform to a game object? 1 Answer