• 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
Question by thenachotech1113 · Feb 13, 2014 at 10:57 PM · error-messagesending object

how do i check if an object has a parent

hello everyone, i want to be able to check if t$$anonymous$$s object has a parent, if so i want it to do send some messages to that parent, but that object could also have no parent, in w$$anonymous$$ch case i want it to do carry out other functions. but not sending messages to its parent. The first t$$anonymous$$ng i came up with was the following

 if (transform.parent.parent != null){
     transform.parent.sendMessage("bla bla");
 } 

that for some reason is giving me a null reference exeption on the first line of the if statement. i have no idea what to try next. any help is much apreciated. thank you all in advanced

Comment
Uila
Fewpwew130

People who like this

2 Show 2
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 Phles · Feb 13, 2014 at 11:06 PM 2
Share

You are checking the parent of the parent with that code, otherwise it should work ok.

avatar image thenachotech1113 · Feb 13, 2014 at 11:15 PM 0
Share

yes, i am trying to check the parent of teh parent to, did not think there would be a diference

5 Replies

· Add your reply
  • Sort: 
avatar image

Answer by homer_3 · Feb 13, 2014 at 11:19 PM

If you are checking for both the parent and parent's parent

 if (transform.parent != null && transform.parent.parent != null){
     transform.parent.sendMessage("bla bla");
 }

Also, the order of t$$anonymous$$s check matters.

Comment
chelder
ElijahShadbolt
Jacky-Marley
AbdurrahmanKhallouf
Uila
daviddickball
tanamu
Hellium
Fewpwew130
n4tm

People who like this

8 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 thenachotech1113 · Feb 13, 2014 at 11:22 PM 0
Share

thank you, i have been cracking my head all day, but i just noticed as i read your answer, if it has no parent, there is no way on earth it could have a parent's parent.

avatar image ElijahShadbolt thenachotech1113 · Nov 15, 2016 at 05:37 AM 0
Share

[Make it best answer] :)

avatar image tanamu · Apr 08, 2018 at 11:04 AM 2
Share

This answer helped me when I tried to check if parent exist by calling transform.parent.gameObject. Calling gameObject like this rises an exception, but if I check transform.parent != null before calling transform.parent.gameObject, everything will be fine. Thanks!

avatar image

Answer by ElijahShadbolt · Jan 27, 2017 at 07:06 AM

For a recursive algorithm:

 Transform parent = transform.parent;
 int i = 1;
 w$$anonymous$$le (parent != null)
 {
     Debug.Log("Reached parent " + i + ": " + parent.name);
     parent.SendMessage("example");
 
     parent = parent.parent;
     ++i;
 }
 Debug.Log("No more parents");
Comment
laser-cookie
Smithy155
Fewpwew130

People who like this

3 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 NoseKills · Jan 27, 2017 at 07:44 AM 0
Share

Definitely a better, more generic answer than the "current best".

I would just change this so "parent" isn't left null at the end like it is now (if there is at least 1 non null parent). Then you could make this a general purpose method that returns the root-most parent if any.

avatar image ElijahShadbolt · Jan 28, 2017 at 07:14 AM 0
Share

@NoseKills Like so:

 Transform parent = transform.parent;
 if (parent != null)
 {
     int i = 1;
     while (true)
     {
         Debug.Log("Reached parent " + i + ": " + parent.name);
         parent.SendMessage("...");
 
         if (parent.parent != null)
         {
             parent = parent.parent;
             ++i;
         }
         else
         {
             break;
         }
     }
     Debug.Log("Root object is " + parent.name);
 }
 else
 {
     Debug.Log("This object has no parents");
 }
avatar image

Answer by majaus · Nov 15, 2016 at 08:51 AM

If you have 1 object w$$anonymous$$t 1 parent and chef for the parent 2, no problem, but if check for parent 3 you recive error because 2 no exist, then you can do:

 if (transform.parent)
         {
             Debug.Log("you have 1 parent");
             if (transform.parent.parent)
             {
                 Debug.Log("you have 2 parents");
                 if (transform.parent.parent.parent)
                 {
                     Debug.Log("you have 3 parents");
                     if (transform.parent.parent.parent.parent)
                     {
                         Debug.Log("you have 4 parents");
                         if (transform.parent.parent.parent.parent.parent)
                         {
                             Debug.Log("you have 5 parents");
                             if (transform.parent.parent.parent.parent.parent.parent)
                             {
                                 Debug.Log("you have 6 parents, ask to mama");
                                 // etc...
                             }
                         }
                     }
                 }
             }
         }
Comment
nazifzak17

People who like this

1 Show 1 · 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 hydrix · Jan 25, 2017 at 09:51 AM 3
Share

looks like we got a code Egyptian

avatar image

Answer by unity_24westk · May 02, 2018 at 11:10 PM

looks like we got a code Egyptian.

Comment

People who like this

0 Show 0 · 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

Answer by onetimepad · Jun 12, 2022 at 05:07 PM

Check if A is a parent of B

 bool ParentCheck(Transform c$$anonymous$$ldTransform, Transform parentTransform){
     if(c$$anonymous$$ldTransform.parent!=null){
         if(c$$anonymous$$ldTransform.parent.transform==parentTransform){
             return true;
         }else{
             return ParentCheck(c$$anonymous$$ldTransform.parent,parentTransform);
         }
     }else{
         return false;
     }
 }





Also c$$anonymous$$ldTransform.IsC$$anonymous$$ldOf(parentTrasform) will give

true if t$$anonymous$$s transform is a c$$anonymous$$ld, deep c$$anonymous$$ld (c$$anonymous$$ld of a c$$anonymous$$ld) or identical to t$$anonymous$$s transform, otherwise false.

Comment

People who like this

0 Show 1 · 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 Bunny83 · Jun 13, 2022 at 02:21 AM 0
Share

You should avoid such nesting if if statements. It makes the code less readable. Also parent.transform is pretty pointless since "parent" is a transform and getter the transform of a transform is redundant. I would suggest a method like this:

 public static bool IsChildOf(this Transform childTransform, Transform parentTransform)
 {
     if(childTransform.parent == null)
         return false;
     if(childTransform.parent == parentTransform)
         return true;
     return IsChildOf(childTransform.parent, parentTransform);
 }

Now the code is linear with 3 destinct exits. Utility functions like that should be static so they can be reused much easier. I also made it an extension method (has to be declared in a static class for this). As n extension method you can simply do

 if (someObj.IsChildOf(someParent))

Since "childTransform.parent" is used 3 times inside the method I would probably create a localv variable for it. It would shorten the lines a bit and also improve performance slightly. Reading the parent actually grabs the parent info out of the native code, So caching it locally would be common.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

set gameobject for another script 0 Answers

Unity Game Crashes? 3 Answers

Unknown identifier: 'supportDX11'. Error Fix? 1 Answer

Error on compiling unity headlookController. 0 Answers

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 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