Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Dionysos1111 · Jan 31, 2017 at 05:25 PM · child-to-gameobject

get first active child gameobject

I'm trying to find out how i can get the first active child gameobject from a parent for a couple of hours now.

Closest thing i came up with is this

                                         GameObject firstChild = transform.GetChild(0).gameObject;
                                         firstChild.SetActive(false);

But this returns the first gameObject even if it's not active and i only need the first active one.

Is there any way to accomplish this?

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

4 Replies

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

Answer by ivanparas · Jan 31, 2017 at 08:36 PM

 //C#
 GameObject firstActiveGameObject;
 
 for (int i = 0; i < gameObject.transform.childCount; i++)
 {
      if(gameObject.transform.GetChild(i).gameObject.activeSelf == true)
      {
            firstActiveGameObject = gameObject.transform.GetChild(i);
      }
 }

This will set the first active child of a game object to the firstActiveGameObject variable.

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

Answer by Orion_78 · May 24, 2019 at 01:02 PM

 transform.GetComponentsInChildren<Transform>()[1]

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

Answer by allenallenallen · Jan 31, 2017 at 05:32 PM

 int i = 0;
 while(i < transform.childCount){
     if (transform.GetChild(i).gameObject.active){
         // transform.GetChild(i) is the GameObject you want
     }
     i++;
 }
 // If the code reaches here, that means there is either no active child or no child at all. You have to handle this yourself.
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
avatar image
0

Answer by nerocraftmc4 · May 26, 2018 at 07:28 AM

i need help. "you cannot implicitly convert the type UnityEngine.Transform to UnityEngine.GameObject @ivanparas ,I copied the code and it's giving me an error. I've been trying this for two days in a row, could any1 help? "You cannot convert the type "UnityEngine.Transform" in "UnityEngine.GameObject" @allenallenallen @ivanparas

Comment
Add comment · Show 1 · 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 TheBernardGroup · Aug 06, 2018 at 05:46 PM 0
Share

I'm not sure if this is going to be far too late, but the problem you are having is that you are missing a small bit off line 8:

firstActiveGameObject = gameObject.transform.GetChild(i);

It should actually read: firstActiveGameObject = gameObject.transform.GetChild(i).gameobject;

you were just a layer too high up, looking at the transform of the parent, and not the gameobject(child).

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I move both the parent and child to a known position where I want the child to be? 1 Answer

How to get the childrens tag from the parent GameObject? 1 Answer

How to hide GUI and chilren? 0 Answers

adding collider to a child object (script) 1 Answer

grandchild of an object not showing in prefab after apply 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