Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
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 this 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 which case i want it to do carry out other functions. but not sending messages to its parent. The first thing 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
Add comment · 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
8

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 this check matters.

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

[$$anonymous$$ake 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
3

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

For a recursive algorithm:

 Transform parent = transform.parent;
 int i = 1;
 while (parent != null)
 {
     Debug.Log("Reached parent " + i + ": " + parent.name);
     parent.SendMessage("example");
 
     parent = parent.parent;
     ++i;
 }
 Debug.Log("No more parents");
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 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

@Nose$$anonymous$$ills Like so:

 Transform parent = transform.parent;
 if (parent != null)
 {
     int i = 1;
     while (true)
     {
         Debug.Log("Reached parent " + i + ": " + parent.name);
         parent.Send$$anonymous$$essage("...");
 
         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
1

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

If you have 1 object whit 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
Add comment · 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
0

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

looks like we got a code Egyptian.

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
0

Answer by onetimepad · yesterday

Check if A is a parent of B

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





Also childTransform.IsChildOf(parentTrasform) will give

true if this transform is a child, deep child (child of a child) or identical to this transform, otherwise false.

Comment
Add comment · 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 · yesterday 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.

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

My Blender object causes frame rate drops 1 Answer

Js: UnityEngine.Rigidbody is required, how to fix? 2 Answers

If I try to open a Jscript I get an Error Message 0 Answers

Unity wont start up 0 Answers

How to fix faceplus MoreBlendshapeMeshes error in unity3d v4.6.1 0 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