{"id":20197,"date":"2022-11-09T00:36:01","date_gmt":"2022-11-08T19:06:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/"},"modified":"2022-11-09T00:36:01","modified_gmt":"2022-11-08T19:06:01","slug":"solved-get-number-of-children-a-game-object-has-via-script","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/","title":{"rendered":"[Solved] Get number of children a game object has via script"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-40994551\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"40994551\" data-parentid=\"40993722\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><strong>EDIT 1<\/strong>:<\/p>\n<p>A simple variable <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Transform-hierarchyCount.html\"><code>transform.hierarchyCount<\/code><\/a>, has been added to <em>Unity 5.4<\/em> and above. This should simplify this.<\/p>\n<p><strong>OLD answer for Unity 5.3 and Below<\/strong>:<\/p>\n<p><code>transform.childCount<\/code> provided by Adrea is usually the way to do this but it does not return a child under the child. It only returns a child that is directly under the GameObject <code>transform.childCount<\/code> is been called on. That&#8217;s it.<\/p>\n<p>To return <strong>all<\/strong> the child GameObjects, whether under the child of another child which is under another child then you have to do some more work.<\/p>\n<p>The function below can count child:<\/p>\n<pre><code>public int getChildren(GameObject obj)\n{\n    int count = 0;\n\n    for (int i = 0; i &lt; obj.transform.childCount; i++)\n    {\n        count++;\n        counter(obj.transform.GetChild(i).gameObject, ref count);\n    }\n    return count;\n}\n\nprivate void counter(GameObject currentObj, ref int count)\n{\n    for (int i = 0; i &lt; currentObj.transform.childCount; i++)\n    {\n        count++;\n        counter(currentObj.transform.GetChild(i).gameObject, ref count);\n    }\n}\n<\/code><\/pre>\n<p>Let&#8217;s say below is what your hierarchy looks like:<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png\" alt=\"enter image description here\"><\/a><\/p>\n<p>With a simple test script:<\/p>\n<pre><code>void Start()\n{\n    Debug.Log(\"Child Count: \" + transform.childCount);\n    int childs = getChildren(gameObject);\n    Debug.Log(\"Child Count Custom: \" + childs);\n}\n<\/code><\/pre>\n<p>This is the result between <code>transform.childCount<\/code> and the custom function:<\/p>\n<blockquote>\n<p>Child Count: 2<\/p>\n<p>Child Count Custom: 9<\/p>\n<\/blockquote>\n<p>As, you can see the <code>transform.childCount<\/code> will not count childs of child but only child of the transform. The custom function was able to count <strong>all<\/strong> the child GameObjects.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Get number of children a game object has via script <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] EDIT 1: A simple variable transform.hierarchyCount, has been added to Unity 5.4 and above. This should simplify this. OLD answer for Unity 5.3 and Below: transform.childCount provided by Adrea is usually the way to do this but it does not return a child under the child. It only returns a child that is directly &#8230; <a title=\"[Solved] Get number of children a game object has via script\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/\" aria-label=\"More on [Solved] Get number of children a game object has via script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,538],"class_list":["post-20197","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-unity3d"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Get number of children a game object has via script - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Get number of children a game object has via script - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] EDIT 1: A simple variable transform.hierarchyCount, has been added to Unity 5.4 and above. This should simplify this. OLD answer for Unity 5.3 and Below: transform.childCount provided by Adrea is usually the way to do this but it does not return a child under the child. It only returns a child that is directly ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T19:06:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Get number of children a game object has via script\",\"datePublished\":\"2022-11-08T19:06:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/\"},\"wordCount\":172,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Solved-Get-number-of-children-a-game-object-has-via.png\",\"keywords\":[\"c++\",\"unity3d\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/\",\"name\":\"[Solved] Get number of children a game object has via script - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Solved-Get-number-of-children-a-game-object-has-via.png\",\"datePublished\":\"2022-11-08T19:06:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Solved-Get-number-of-children-a-game-object-has-via.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Solved-Get-number-of-children-a-game-object-has-via.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-get-number-of-children-a-game-object-has-via-script\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Get number of children a game object has via script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Get number of children a game object has via script - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Get number of children a game object has via script - JassWeb","og_description":"[ad_1] EDIT 1: A simple variable transform.hierarchyCount, has been added to Unity 5.4 and above. This should simplify this. OLD answer for Unity 5.3 and Below: transform.childCount provided by Adrea is usually the way to do this but it does not return a child under the child. It only returns a child that is directly ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/","og_site_name":"JassWeb","article_published_time":"2022-11-08T19:06:01+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Get number of children a game object has via script","datePublished":"2022-11-08T19:06:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/"},"wordCount":172,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png","keywords":["c++","unity3d"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/","url":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/","name":"[Solved] Get number of children a game object has via script - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png","datePublished":"2022-11-08T19:06:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/11\/Solved-Get-number-of-children-a-game-object-has-via.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-get-number-of-children-a-game-object-has-via-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Get number of children a game object has via script"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1779427625","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20197"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20197\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}