- Home /
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
You are checking the parent of the parent with that code, otherwise it should work ok.
yes, i am trying to check the parent of teh parent to, did not think there would be a diference
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.
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.
[$$anonymous$$ake it best answer] :)
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!
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");
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.
@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");
}
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...
}
}
}
}
}
}
Answer by onetimepad · 20 hours ago
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.
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
Follow this Question
Related Questions
Android Calling? 0 Answers
Why does Unity keep crashing? 0 Answers
Random null reference exception error. 0 Answers