- Home /
GameObjectWithTag Child
Question, how do I find a gameobject with tag which is a child of a gameobject? For some reason gameobjectwithtag doesnt works the same as gameobject.find... Well? Many thanks.
Comment
Best Answer
Answer by save · Jul 11, 2012 at 01:58 AM
Good question and upvoted! I made a sample script that solves the issue hopefully.
#pragma strict
#pragma downcast
var parent : Transform;
function Start () {
var childObject : GameObject = FindChildGameObjectWithTag(parent, "MyTag");
if (childObject) Debug.Log(childObject.name+" was found with your tag");
}
function FindChildGameObjectWithTag (p : Transform, tagName : String) : GameObject {
for (var child : Transform in p) {
if (child.tag==tagName) {
return child.gameObject;
}
}
return null;
}