Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
13
Question by David 3 · Mar 31, 2010 at 03:02 PM · gameobjectprefabchildrenvisibility

Show and Hide a prefab or GameObject

Hi,

Does anyone have a script to show or hide a prefab (or game object) on a key press?

The prefab contains multiple objects which should all be shown or hidden at once.

thanks

david

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 cregox · Sep 06, 2011 at 06:04 PM 0
Share

if you mean to completely disable it: http://answers.unity3d.com/questions/35363/gameobjectactive-not-deactivating-all-children.html

10 Replies

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

Answer by duck · Mar 31, 2010 at 03:09 PM

You can't show/hide a prefab as such, because a prefab is like a "template" of an object which could exist in your scene. You probably want to hide/show a gameobject (which might or might not be an instance of a prefab).

Do do this, you set the GameObject's renderer.enabled property to either true or false, like this:

function Update() {

 if (Input.GetKeyDown(KeyCode.Z)) {
     // show
     renderer.enabled = true;
 }

 if (Input.GetKeyDown(KeyCode.X)) {
     // hide
     renderer.enabled = false;
 }

}

or, to toggle the visibility on and off with a single keypress:

function Update() {

 if (Input.GetKeyDown(KeyCode.Z)) {
     // toggle visibility:
     renderer.enabled = !renderer.enabled;
 }

}

If you need to change the visibility of a GameObject and its children, you can loop through every object and change each one, like this:

function Update() { if (Input.GetKeyDown(KeyCode.Z)) { ToggleVisibility(); } }

function ToggleVisibility() { // toggles the visibility of this gameobject and all it's children var renderers = gameObject.GetComponentsInChildren.(); for (var r : Renderer in renderers) { r.enabled = !r.enabled; } }

(moved the toggling code to a separate function, which is often a good idea once any particular section of code starts growing significantly in size)

Comment
Add comment · Show 12 · 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 David 3 · Mar 31, 2010 at 03:29 PM 0
Share

Thanks for an extremely quick reply! those scripts work well for single meshes :)

however my prefabs have multiple meshes - how could i apply it to an entire instance of a prefab? [let me know if the proper etiquette on this site is to post a new question or not]

avatar image duck ♦♦ · Mar 31, 2010 at 03:42 PM 0
Share

if the question is just a small addendum to the original question (like yours is), a comment is best. If it's more like "the next question" in your larger set of goals, a new question is best.

avatar image duck ♦♦ · Mar 31, 2010 at 03:47 PM 0
Share

edited to include gameobjects with children (I'm guessing this is what you meant by 'multiple meshes', right?)

avatar image duck ♦♦ · Mar 31, 2010 at 04:28 PM 0
Share

doh! accidentally dropped a bit of C# in there! Yes, changing 'bool' to 'var' will fix that. But also, the script assumed that the 'parent' gameobject had a renderer, wheras I'm guessing your's doesn't (only the children have them?). I have fixed that now too (which completely removes the need for that var anyway!) Duck 37 secs ago

avatar image duck ♦♦ · Mar 31, 2010 at 04:29 PM 0
Share

ok - yeh I use c# for work, but tend to go with JS when helping out (if no preference is specified) because most c# people can convert from JS, but not the other way around! ;)

Show more comments
avatar image
17

Answer by vbs · Jun 08, 2015 at 05:54 AM

For Unity 5, you access game object's rendered component with this syntax:

gameObject.GetComponent().enabled = false;

So the above answer would be:

 function Update() {
 
  if (Input.GetKeyDown(KeyCode.Z)) {
      // show
      // renderer.enabled = true;
      gameObject.GetComponent<Renderer>().enabled = true;
  }
  
  if (Input.GetKeyDown(KeyCode.X)) {
      // hide
      // renderer.enabled = false;
      gameObject.GetComponent<Renderer>().enabled = false;
  }
 }
 
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 Lao2211 · Apr 09, 2016 at 12:20 AM 0
Share

For Unity 5.X this is the answer that works. The editor will recognize the old code as legacy and offer you to convert to the new syntax based on components.

avatar image
1

Answer by Gardemarinas · Jun 23, 2012 at 10:17 PM

Whit C# to disable Textures I use this code:

 public void ChangeMenuActivity(bool state)
 {
     ToggleVisibility(gameObject.transform, state);
 }

 void ToggleVisibility(Transform obj, bool state)
 {
     for (int i = 0; i < obj.GetChildCount(); i++)
     {
         if (obj.GetChild(i).guiTexture != null)
             obj.GetChild(i).guiTexture.enabled = state;
         if (obj.GetChild(i).guiText != null)
             obj.GetChild(i).guiText.enabled = state;
         
         if (obj.GetChild(i).GetChildCount() > 0)
         {
             ToggleVisibility(obj.GetChild(i), state);
         }
     }
 }
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 cregox · Jun 25, 2012 at 04:34 PM 0
Share

You really should ins$$anonymous$$d adapt Ben's code into C# - it should perform much better. And, since I'm already criticizing your code, that's setting, not toggling visibility. Good na$$anonymous$$g can make a lot of difference.

avatar image nfrrtycmplx · Jul 09, 2014 at 07:26 AM 0
Share
     public void Hidden(bool isHidden)
     {
         Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>();
         foreach (Renderer r in renderers)
         {
             r.enabled = !isHidden;
         }
     }
 


This is the adaptation into c# that I use, and it works well.

avatar image
1

Answer by JxDarkAngel · Dec 19, 2012 at 12:13 AM

Tambien se puede hacer de la siguiente forma:

using System.Reflection;

void OcultarPadreHijos(object objGO) {

  PropertyInfo Propiedad= objGO.GetType().GetProperty("enabled");

  Propiedad.SetValue(objGO,false,null);

}

Si quieres ocultar a sus componentes hijos, solo mandas a llamar a la función por cada hijo

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 gnoren · Apr 22, 2014 at 12:58 AM

Hey I know this is a pretty old thread, but I was hoping someone might have a bit of insight into a problem I'm having with this code (the last Toggle example) I am very new to Unity and am probably missing something obvious. I saved this as a script and childed it to the object (platforms) which I want to toggle:

But keep getting this error: Assets/Scripts/platforms_o_off.js(19,55): BCE0043: Unexpected token: ..

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 
 if (Input.GetKeyDown(KeyCode.Z)) {
         ToggleVisibility();
     }
 
 }
 
 
 function ToggleVisibility() {
     // toggles the visibility of this gameobject and all it's children
     var renderers = gameObject.GetComponentsInChildren.(platforms);
     for (var r : Renderer in renderers) {
         r.enabled = !r.enabled;
     }
 }
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 alex2018 · Dec 21, 2014 at 07:44 PM 0
Share

hello I would like to know how come and go with the same key a model .fbx thank you very much

  • 1
  • 2
  • ›

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

15 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

Related Questions

Adding Prefab Components 1 Answer

Attaching different prefabs via code? 2 Answers

Inventory Item preview 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Zoom In: Reveal hidden object - help 3 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