- Home /
Finding Children question
To get a child to the current gameobject, is there a difference between transform.Find and transform.FindChild?
I'm also confused about how (or if) I can disable a GameObject using its Transform. I've written the below but get an error ( An instance of type 'UnityEngine.Transform' is required to access non static member 'Find'. ).
What's the correct (and most efficient) way to write this?:
var barricade : Transform;
function Start ()
{
// Cache it
barricade = Transform.Find("Wall").FindChild("Barricade");
}
function Update ()
{
barricade.active = false;
}
Answer by michael-bartnett · Sep 25, 2012 at 04:38 PM
To address the difference between the `transform.Find` and `transform.FindChild` instance methods, open UnityEngine.dll in MonoDevelop's Assembly Browser. Navigate to the Transform class, and look at the `FindChild` function, and you'll see it's just wrapping `Transform.Find`.
using System;
public Transform FindChild (string name)
{
return this.Find (name);
}
So the answer is: use `Transform.Find`, because `Transform.FindChild` is just a wrapper for the former.
The OP has marked the question as answered - so we must presume that @rutter provided the solution he was looking for.
As neither Find or FindChild are static methods the problem was trying to access them as such.
@whydoidoit I'm realizing my eyes glossed over the non-static function error portion of his question. But he still asked: "Is there a difference between transform.Find and transform.FindChild?" Which is a completely different question from "Why can't I cal Transform.FindChild("somechildname")?" I'll edit my answer to be less hostile.
Yeah you are quite right, that is indeed what the first line says isn't it :)
Hey thanks $$anonymous$$ichael. You're right, I'd asked two questions in my post, so my bad on the confusion.
I actually believe my question was more related to the difference between transform.Find and FindChild so I'm updating it so yours is the appropriate answer — thanks for the input, I still hadn't figured out the difference between them! :)
Answer by rutter · May 14, 2012 at 09:21 PM
Transform
and transform
aren't quite the same thing:
Transform
refers to the classtransform
refers to the instance of that class which happens to be attached to the current GameObject
That's where your error is coming from.
A very similar difference applies between GameObject
and gameObject
.
The rest of your questions can probably be answered by close review of this script reference page, or by checking over the reference pages for the Transform and GameObject classes.
I'd sum up the difference like so:
GameObject.Find() looks for a match within in the entire scene.
transform.Find() looks for a match within the current transform's children.
So it's somewhat a question of how wide of a net you're looking to cast.
Ah, thanks.
So I write it as:
var barricade : GameObject;
function Start ()
{
// Cache it
barricade = transform.Find("Wall").Find("Barricade").gameObject;
}
function Update ()
{
barricade.active = false;
}
Although I still don't understand the difference between transform.Find and transform.FindChild — Aren't they both searching children of the current transform?
I think you're correct on that point. I notice that the official script reference only mentions the one function, which might imply that the other one has been deprecated. I've seen that Unity has a few functions like that left about, which can be confusing.
TY, this is sound simple now, but i didn't encounter this kind of syntax ".gameObject"at the end... transform.Find("Wall").Find("Barricade").gameObject; Do u have more reference .gameObject? Anyway TY ;-)
Answer by whydoidoit · May 14, 2012 at 09:17 PM
You need to be using transform.Find (with a lowercase "t") that accesses the Transform of the current GameObject. You want to deactivate a GameObject using SetActiveRecursively if you want to do something that affects all of the children rather than trying to deactivate the transform. If you use transform.Find("myChild") then use .gameObject to get the item to activate/deactivate.
Your answer
Follow this Question
Related Questions
Instantiate Terrain Object as child of Empty Game Object 1 Answer
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
transform.Find() returns a Transform? 1 Answer
Destroy Child of Game Object 2 Answers
Finding class where current GameObject is assigned to as a var 1 Answer