Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges