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
0
Question by Nk.Andrei · May 30, 2013 at 07:14 PM · listparentchildcompare

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.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

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 Nk.Andrei · May 30, 2013 at 08:14 PM 0
Share

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.

avatar image hiddenspring81 · May 30, 2013 at 08:29 PM 1
Share

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);
   }
 }
avatar image Nk.Andrei · May 31, 2013 at 05:31 AM 0
Share

Got it. It works. Thanks.

avatar image
0

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

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

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

15 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

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


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