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
3
Question by greenlotus · Nov 06, 2012 at 07:18 AM · parentchildreferenceaccess

Best way to Reference Child from Parent's script?

Hi Guys,

Been all over the net and got a lot of info on how to do this - but nothing saying which method is best for performance - nor why to use a particular method over another.

Also, I am confused as to why a child object cannot be directly referenced in its parents script?

If I have a parent object Man with child HandL -

Why is it that in the Man class,

this.HandL.transform.localPosition = (new Vector3(0,1.3f,0));

Does not work??! - It says, HandL is not in current context??

Is this because HandL was added to the parent, man, in the interface? Is there a way to declare that HandL is a child of man - and then reference it directly using

this.HandL.transf......

//-------------------------------------

Otherwise I have found three ways to do this - which is best for performance? esp on smartphones?

//--------------------------- var hand = GameObject.Find("HandL"); hand.transform.localPosition = (new Vector3(0,1.3f,0)); //---------------------------- //--------------------------- var hand = transform.Find("HandL"); hand.transform.localPosition = (new Vector3(0,1.3f,0)); //--------------------------

//-------------------------- other.GetComponent().waving(); //-------------------------- //-- in this last one the moving is done in the hand class

As a noob I find this simple stuff really confusing. Please let me know which way is best and also if there is a simple way to get direct access to my children from the parent class.

Thanx,

GreenLotus.

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
6
Best Answer

Answer by Eric5h5 · Nov 06, 2012 at 07:23 AM

The child can't be directly referenced like that because the child doesn't exist in your code, it exists in the Unity project. Therefore you need to get a reference to it. As for performance, it's completely irrelevant, since you're only doing it once (or you should be). You should probably use transform.Find in this case, because GameObject.Find will just get the first object it finds with that name, which isn't necessarily the child you want. Using transform.Find makes it clear that you're getting a child, not just some other game object in the scene.

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
avatar image
5

Answer by Sonaten · Nov 06, 2012 at 12:25 PM

I access objects and children in my scenes like this (I know I'm not addressing if it is the best method or not, but I believe it to work quite well).

 private GameObject sceneParent, myRocket;
 
 void Start () {
     sceneParent = GameObject.Find("SceneParent");
     myRocket = GameObject.Find("Raket");
     myRocketDeepChild = myRocket.transform.GetChild(0).GetChild(0).GetChild(1).gameObject;
     //the Deep child is one of the indermost children in the structure. 
     //It is simply a container for graphics that I want to handle.
     }

This way, I keep the important children in variables (pointers really), and in any other case I would just use transform.GetChild(index).

Take note that GameObject.Find searches all objects in the scene, so I'd suggest not using it in run time; and, prefereable before any prefabs are instantiated.

I hope you can use this.

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
avatar image
2

Answer by Xathereal · Nov 06, 2012 at 01:01 PM

Finding things by name is generally a bad thing to do, but sometimes (like in this situation) it is a viable solution.

Try this code which is based upon the fact that every gameObject has a transform (correct me if I am wrong please):

 Transform[] transforms = this.GetComponentsInChildren<Transform>();
 
 foreach(Transform t in transforms)
 {
     if (t.gameObject.name == "Child")
     {
         Debug.Log ("Found " + t);
     }
 }

Hope this helps! =D

Comment
Add comment · Show 2 · 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 aklgupta · Jan 16, 2019 at 12:01 PM 0
Share

Does this have any advantages over the "Find" function of transform? Is there any other difference?

avatar image Boolean_of_True_th · Dec 16, 2020 at 03:24 PM 0
Share

I guess the real difference is that it would be better if you had many child objects with the same name you would be able to reference all of them. e.g. a herd of monsters that attack in a group but only if you enter their territory. I don't think it's any different in this case though.

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

14 People are following this question.

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

Related Questions

Make a simple tree 1 Answer

Using Tag to find Child and execute Scripts 1 Answer

Accessing a variable from a child object and pass it to the parent 2 Answers

Access parents other child from other other child 1 Answer

Access a child from the parent or other gameObject. 2 Answers


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