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
0
Question by Cyrax756 · Nov 05, 2014 at 01:54 AM · c#

Getting all children of 3d model at once

Attempting to grab all children of my 3d model and make it dissappear at certain times, works as is, but curious if there is a neater way to go about it without declaring all those variables. May need to use this script with 2 components, such as Mesh renderer and a collider. That would requiring 20 variables total and way too messy for my taste. Thanks in advance.

 public class DummyDissappear : MonoBehaviour {
     public LightFlicker LightFlickerScript;
     public Renderer render, render1, render2, render3, render4, render5,render6,render7,render8,render9,render10;
 
 
 
     
     //This Script uses Light Flicker script to make a GameObject dissappear
     //May need tweak depending on the GameObject this is applied to
     void Start () {
         GetComponent<LightFlicker> ();//LightFlicker script
 
     
     }
     
 
     void Update () {
         if (LightFlickerScript.enabled == false) //LightFlicker script is off
         {
             render1.enabled = false;//Disable mesh render off all selected children
             render2.enabled = false;
             render3.enabled = false;
             render4.enabled = false;
             render5.enabled = false;
             render6.enabled = false;
             render7.enabled = false;
             render8.enabled = false;
             render9.enabled = false;
             render10.enabled = false;
         }
             
         
         if (LightFlickerScript.enabled == true) //LightFlicker Script is on
         {
             
             render1.enabled = true;//Enable mesh render off all selected children
             render2.enabled = true;
             render3.enabled = true;
             render4.enabled = true;
             render5.enabled = true;
             render6.enabled = true;
             render7.enabled = true;
             render8.enabled = true;
             render9.enabled = true;
             render10.enabled = true;
         }
         
     }
 }
Comment
Add comment · Show 1
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 slay_mithos · Nov 05, 2014 at 02:04 AM 0
Share

I'm not completely certain I understand the question right, but I'll try anyway. Just disregard it if it's not what you meant.

Why not use a "List" as the type, and add your renderers into in?

That way, you can just do a simple foreach loop, or a Linq request to alter all the objects easily.

1 Reply

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

Answer by Ricewind1 · Nov 05, 2014 at 02:01 AM

The code below de-activates all children of the mentioned gameobject's transform

 foreach (Transform child in transform)
             {
                 child.gameObject.SetActive(false);
             }

Or to reference them for future use:

     private List<Transform> children;
     void Start()
     {
         children = new List<Transform>();
         foreach (Transform t in this.transform)
         {
             children.Add(t);
         }
     }
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 Cyrax756 · Nov 05, 2014 at 02:08 AM 0
Share

So obvious now, thank you! I tried the foreach and couldn't get it to work before.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Controlling when a script is enabled with another Script 1 Answer

Getting index of the smallest number in a list 1 Answer

How big is too big? Terrain Question. 1 Answer

How to transfer colllider from OnTriggerEnter to Update 1 Answer

Help| Convert a javascript to C# 1 Answer


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