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
4
Question by Digital-Phantom · Feb 05, 2015 at 02:41 PM · javascriptsetactivechild objectenabledgame object

Set Objects Child to Active/Inactive(Solved)

Part of my script allowed for a gameObject to be set active/inactive depending if it was selected(or not)

 if(selected)
     {
         
         // Activate/Deactivate the game object/child.
         gameObject.SetActive(true);
     }
 

Things have changed a little now and that gameObject has a single child that I now want to make active/inactive. There seems to be so many conflicting ideas about how to do this its confusing.

Whats the simplest way to access the child object and deactivate it. (The parent object is always active)

???

Comment
Add comment · Show 2
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 Digital-Phantom · Feb 05, 2015 at 03:41 PM 0
Share

@chariot if you want to replace your comment as an answer I'd be happy to up vote it.

avatar image LilGames Digital-Phantom · Mar 13, 2021 at 02:34 PM 0
Share

Converted to answer

5 Replies

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

Answer by chariot · Feb 05, 2015 at 03:41 PM

My answer, glad that it helps you

 private var childObj : Transform;
  
  function Start () {
    childObj = transform.Find("Child Name");
  }
  
  function Update () {
    if (selected){
      childObj.active = true;
    }
  }
Comment
Add comment · Show 2 · 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 LilGames · Oct 17, 2018 at 08:18 PM 1
Share

isn't .active deprecated?

avatar image sniper02311 LilGames · Jan 20, 2019 at 01:57 PM 0
Share

Yes, it is not even an option anymore that I see.

avatar image
47

Answer by PrisVas · Feb 05, 2015 at 03:05 PM

Try

 transform.GetChild(0).gameObject.SetActive(false);
Comment
Add comment · Show 18 · 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 Digital-Phantom · Feb 05, 2015 at 03:41 PM 0
Share

didn't work

avatar image Nyan_Cat123 Digital-Phantom · Mar 13, 2021 at 01:37 PM 0
Share

public bool open = false; // Start is called before the first frame update void Start() {

 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown("q"))
     {
         if (open)
         {
             for (int a = 0; a < transform.childCount; a++)
             {
                 transform.GetChild(a).gameObject.SetActive(false);
             }
             open = false;
         } else
         {
             for (int a = 0; a < transform.childCount; a++)
             {
                 transform.GetChild(a).gameObject.SetActive(true);
             }
             open = true;
         }
     }
 }

here i think this will work

avatar image Dartmor · Jun 11, 2015 at 06:10 AM 0
Share

worked for me, thnx PrisVas !

avatar image DiegoMontania · Sep 07, 2015 at 11:07 PM 0
Share

WOW $$anonymous$$AN! This work for me, thanks!

avatar image 26PM · Sep 09, 2015 at 11:45 AM 0
Share

Thank you! Worked like a charm and so simple!

avatar image PrisVas · Jun 06, 2017 at 07:23 PM 4
Share

For multiple children I would use a loop. Would be something like:

 for (int a = 0; a < transform.childCount; a++)
 {
      transform.GetChild(a).gameObject.SetActive(false);
 }
Show more comments
avatar image
6

Answer by AmineBMA · Aug 13, 2017 at 06:22 PM

parentGameObject.transform.GetChild(0).gameObject.SetActive(false);

Comment
Add comment · Show 2 · 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 virus4297 · Nov 05, 2020 at 11:09 AM 0
Share

I have 3 GameObjects namely red, blue and green, and each has children objects named A1, A2, A3....A100. (all 3 objects have children of the same name).

How do I disable one specific object? Say A5 of Red, and A7 of Blue.

avatar image virus4297 virus4297 · Nov 05, 2020 at 12:03 PM 0
Share

okay was able to solve it, thanks @A$$anonymous$$eB$$anonymous$$A

 GameObject Red,Blue,Green;
  Red.transform.Find("A5").gameObject.SetActive(false);







avatar image
0

Answer by mikembley · Feb 05, 2015 at 03:31 PM

         if (selected)
             theChild.SetActive(true); // Activate
         else
             theChild.SetActive(false); //Deactivate
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 Digital-Phantom · Feb 05, 2015 at 03:40 PM

I actually came up with a solution based on your suggestion.

 private var lightRing : Projector;
     
     function Start ()
     {
         lightRing = GetComponentInChildren(Projector);
     }
     
     if(selected)
         {
             lightRing.enabled = true;
         }
 

 
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

38 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

Related Questions

How to enable/Disable/enable the Particle System Component of a child object on the same object/Parent? (Solved) 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How do you call child in group of elements? 2 Answers

Enabling/Deactivating objects using keys. 1 Answer

How to setActive(true) then false on enemy child gameobject based on closest enemy to player? (Closest Enemy Target System) 0 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