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
1
Question by KennSan · May 31, 2011 at 06:43 AM · gameobjectparentinvisiblevisibility

Make code effect children of a game object

I'm looking to set several objects to invisible but I need to do it through the parent to which the meshes are children. Is there any way I could use the renderer.enabled=false; to effect all of the children from the parent?

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
1

Answer by iggy · May 31, 2011 at 07:25 AM

 var meshChildren : MeshRenderer[];
 meshChildren = gameObject.GetComponentsInChildren.<MeshRenderer>();
 for (var mesh : MeshRenderer in meshChildren) {
     if(mesh.gameObject!=gameObject) 
         mesh.enabled = false;
 }

source: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Edit: i added a fix thanks to Christian. I tested the code and it works. Hope it helps.

Comment
Add comment · Show 4 · 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 CHPedersen · May 31, 2011 at 09:07 AM 2
Share

That will work, but beware that GetComponentsInChildren is horribly, horribly misnamed, since it also returns components from the parent object, and not just its children, which one might've otherwise been led to believe. ;)

So unless you mean to disable the parent's renderer as well, you need to check the returned renderers to make sure you're only disabling children.

avatar image iggy · May 31, 2011 at 09:47 AM 0
Share

you are right :) thanks, i added a fix to original post

avatar image KennSan · May 31, 2011 at 07:40 PM 0
Share

Does this block of code get called all at once? I shoved it in the update function of a gameobject that is calling it and everything still appeared. I apologize if there's something obvious I'm not understanding.

Edit: I also made a Debug.Log call within the same if statement for the mesh.enabled = false to take effect, but it doesn't seem to show up.

avatar image iggy · May 31, 2011 at 11:25 PM 0
Share

you must put this code in some function first and call that function when you want to hide it.

this is my "HideChildren.js". i add it to parent and when i press "spacebar" it calles the function and hides all children.

function Update () { if (Input.Get$$anonymous$$eyDown ("space")) hideChildren(); }

function hideChildren() { var meshChildren : $$anonymous$$eshRenderer[]; meshChildren = gameObject.GetComponentsInChildren.<$$anonymous$$eshRenderer>(); for (var mesh : $$anonymous$$eshRenderer in meshChildren) { if(mesh.gameObject!=gameObject) mesh.enabled = false; } }

avatar image
0

Answer by KennSan · Jun 01, 2011 at 04:48 PM

I can't work out why but using the code that time Iggy spat out a load of errors out at me. However upon digging a lot deeper I found something that is very like your code, but there are some minor code differences:

function Update() {

if (Input.GetKeyDown(KeyCode.Z)) {

 DoRender();

} }

function DoRender(){ var children : Renderer[]; children = GetComponentsInChildren.(); Debug.Log (children); for (var i : Renderer in children) { i.enabled = false; } }

However I do thank you for all your patience and help =) If you didn't help I wouldn't have had a clue when I was even warm to being correct!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Game Objects appear in Scene but not in Game view 2 Answers

Can one specify the parent of a gameobject in an array? 2 Answers

Moving a GameObject reffering to the coordinates of the parent Object 1 Answer

Is there a way to make a selected object a child of another object during the game? 1 Answer

How can I attach a gameobject to another without changing its scale. 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