- Home /
Find out if two children/gameObjects have the same parent.
I am trying to find out if two or more gameObjects within a list have the same parent by comparing them.
Example :
  if(child1.transform.parent == child2.transform.parent)
  {
   /////do this
  }
 
    or
 
  childList : List.<Transform> = new List.<Transform>();
  currentObject : Transform; ///the current gameObject
 
 
  if(childList[i].transform.parent ==    gameObject.transform.parent)//////the children in childList and the currentObject have the same parent
  {
    //////do this`enter code here`
  }
Any help is greatly appreciated.
Answer by hiddenspring81 · May 30, 2013 at 07:23 PM
Here's some sample code to help push you in the right direction.
 bool ShareSameParent (IEnumerable<Transform> childList)
 {
   foreach (var child1 in childList)
   {
     foreach (var child2 in childList)
     {
       if (child1 == child2)
         continue;
       if (child1.parent == child2.parent)
         return true;
     }
   }
 
   return false;
 }
I haven't tested this code, and it's written in C#, but it should be very simple to convert to JavaScript.
Thanks for the reply but it wont work because it gives an error if I try to remove the child from the list.I have tried indexing the list with a for (i) but it still dosent let me pass to the next line.If I compare child1.parent to child2.parent or childList[i].parent to gameObject.parent it wont let me pass.
If I say : if(childList[i].transform.parent ==gameObject.transform.parent) { print("a"); } //considering that the children in the list and the gameObject have the same parent It wont print the line.
You cannot modify the content of a List while you are enumerating it. If you want to remove children from the List during enumeration, then try the following,
 void ShareSameParent (List<Transform> childList)
 {
   var remove = new List<Transform>();
   foreach (var child1 in childList)
   {
     foreach (var child2 in childList)
     {
       if (child1 == child2)
         continue;
       if (child1.parent == child2.parent)
       {
         // Add it to a list of children to removed later
         remove.Add(child1);
         continue;
       }
     }
   }
 
   // Remove the children from the List
   foreach (var child in remove)
   {
     childList.Remove(child);
   }
 }
Answer by Nk.Andrei · May 30, 2013 at 08:11 PM
 Thanks for the reply but I have already tested something similar by comparing child1.parent to child2.parent or  child.parent to gameObject.parent but it never lets me pass to the next line.
If I say :
 if(childList[i].transform.parent == gameObject.transform.parent)
 {
  print("a");
 }
 //considering that the children in the childList and the gameObject have the same parent
It wont print the line
Your answer
 
 
             Follow this Question
Related Questions
Make a simple tree 1 Answer
Cannot cast from source type to destination type. 1 Answer
A node in a childnode? 1 Answer
Adding children to the list during the game. 1 Answer
Child cant apply variables from parent 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                