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;
}
Transform
and transform
aren’t quite the same thing:
-
Transform
refers to the class
-
transform
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:
So it’s somewhat a question of how wide of a net you’re looking to cast.
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.
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.