• 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
5
Question by Jordan Miller 2 · Oct 08, 2010 at 12:35 AM · parentchildbce0048

how to see if transform has child?

is there a simple code to access the first child of a parent I know about

for (var child : Transform in transform) {
    // do whatever you want with child transform here
}

but I kinda want something like this

if (var child : Transform in transform)
{
//do this
}

because I just have one child under this object if there is one at all. or maybe a way to just see if transform is a parent or not?

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
21
Best Answer

Answer by skovacs1 · Oct 08, 2010 at 07:09 PM

If a transform has a child

if(transform.childCount > 0) //do something

will only execute something if there is a child.

Foreach

for (var child : Transform in transform) {
    // do whatever you want with each child transform here
}

doesn't access just the first child, but rather every immediate child of the transform. If you have no children in the transform, it will never go into this block. It is semantically the same as:

for(var i : int = 0; i < transform.childCount; i++) {
    var child : Transform = transform[i];
    // do whatever you want with each child transform here
}

which will check the loop condition before performing any iterations. If transform.childCount is 0, then i >= transform.childCount when i = 0 on the first iteration and the loop will exit before doing anything.

In JavaScript, the foreach loop which loops through every something in something else is written as:

for(var something : Thing in somethingElse) {
    // do whatever you want with each something here
}

which is semantically the same as:

for(var i : int = 0; i < somethingElse.length; i++) {
    var something : Thing = somethingElse[i];
    // do whatever you want with each something here
}
Comment
Add comment · Show 6 · 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 Jordan Miller 2 · Oct 08, 2010 at 07:23 PM 0
Share

this is awesome! thanks!

avatar image user-5759 (google) · Oct 22, 2010 at 10:48 PM 0
Share

Doesn't seem to work, generated error: Type 'UnityEngine.Transform' does not support slicing.

avatar image skovacs1 · Oct 25, 2010 at 01:59 PM 0
Share

Could you be more specific as to which portion of code is generating this error for you? Bear in $$anonymous$$d that just because the foreach version for Transforms version is semantically the same as as for loop does not entail that the for loop is legal code.

avatar image misterlee · Mar 02, 2015 at 08:01 PM 1
Share

That was just what I needed! Thanks :)

I ended up using

 if (transform.childCount == 0)

though since I only wanted the action to happen if there were no children.

avatar image afakul misterlee · Jul 31, 2021 at 11:21 AM 0
Share

does we place this at the parent ?

avatar image rage_co afakul · Jul 31, 2021 at 11:44 AM 0
Share

You just need to reference the parent transform and you can use this on any script

avatar image
-2

Answer by hari2015 · Mar 09, 2018 at 07:42 AM

In c# foreach (Transform child in Children) { // whatever }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

stop animations when an other animation is playing 2 Answers

How to detect child object collisions on parent 3 Answers

Instantiating as a child error 1 Answer

How do I allocate a parent to a gameobject that just got instantiated? 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