• 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 corbinyo · Oct 08, 2018 at 05:06 AM · parentliststags

Find GrandChildren of Grandparent Gameobject by tag.

Trying to find the grandchildren of a GameObject by using tags to put in a list of Gameobjects that is attatched to the grandparent gameobject.

However, this code is finding ALL the gameobjects with the tag instead of only the gameobject with the tag that are the grandchildren of the Gameobject.

  foreach (Transform child in transform)
         {
             if (child.tag == "ScaleThisCube")
             {
                 if (!child.Equals(transform))
                 {
                     cubeobjects.Add(child.gameObject); //first list of gameobjects (puts children with tag ScaleThisCube into a list)
                 }
            
              }
         }
 
   
         foreach (Transform child in transform)   // ThisForeach loop is not working however....
         {
             cubeobjects.Add(transform.gameObject);
             foreach (Transform grandChild in child) 
             {
                 if (grandChild.tag == "CubeFace")
                 {
                     if (!grandChild.Equals(child))
                     {
                         cubefaces.Add(grandChild.gameObject); //second list of gameobjects (puts children of gameobject in the list "CubeObjects" with tag CubeFace into a list)
                  
                     }
                 }
             }
         }


Comment
Add comment · Show 3
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 hexagonius · Oct 08, 2018 at 07:10 AM 0
Share

ok, so why is CubeObjects not a list of Transforms? why does the second loop not iterate CubeObjects but again the Transforms?

avatar image corbinyo · Oct 08, 2018 at 02:12 PM 0
Share

CubeObjects is a list of gameobjects because I need to iterate over that list for a specific function. And, I thought I was iterating over the cubeobjects list?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bennett_apps · Oct 08, 2018 at 11:13 PM

try using this logic:

 transform.GetChild(indexOfChild).gameObject.FindWithTag("WhateverTagYouWant");

You may have to tweak this to your personal needs :D

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.

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

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

Related Questions

Convert tags to list, then back to array 1 Answer

Using parent tag on whole gameObject 1 Answer

Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers

Make a simple tree 1 Answer

How to not have a function affect a prefab? 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