• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Essential · May 14, 2012 at 08:44 PM · gameobjecttransformchildfind

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Sep 25, 2012 at 04:54 PM 0
Share

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.

avatar image michael-bartnett · Sep 25, 2012 at 04:59 PM 0
Share

@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.

avatar image whydoidoit · Sep 25, 2012 at 05:01 PM 0
Share

Yeah you are quite right, that is indeed what the first line says isn't it :)

avatar image Essential · Sep 26, 2012 at 03:47 AM 0
Share

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! :)

avatar image
2

Answer by rutter · May 14, 2012 at 09:21 PM

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:

  • 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · May 14, 2012 at 09:32 PM 0
Share

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?

avatar image rutter · May 14, 2012 at 10:27 PM 0
Share

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.

avatar image amit-chai · Mar 11, 2015 at 04:13 PM 0
Share

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 ;-)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

This does not make sense this.gameObject.transform.Find() why? 2 Answers

Instantiating a new gameObject as a child of a different gameObject 2 Answers

transform.Find(string)? 2 Answers

GameObjectWithTag Child 1 Answer

GameObject.Find("something")..how to use with transform,audiosource and texture ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges