• 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
1

Answer by Andreywk · Nov 16, 2015 at 04:34 PM

This can be useful for someone. Hide / Show UI objects for example Menu.

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

 private void ToggleVisibility(Transform obj, bool state) {
     Image img = obj.GetComponent<Image>();
     if (img != null)
         img.enabled = state;
     Text txt = obj.GetComponent<Text>();
     if (txt != null)
         txt.enabled = state;            
     foreach (Transform tr in obj) 
         ToggleVisibility(tr, state);        
 }
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 thienhaflash · May 25, 2014 at 10:02 AM

Seems to be the only correct answer around, I will check it, but at least from reading the code what I guest It will work. Thanks a lot

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 Brendo13 · Feb 15, 2016 at 05:07 AM

For Unity 5: You could also use the SetActive(bool) function

(name of Gameobject reference).SetActive(false);

for example:

car1.SetActive(false);

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 SibochCZ · May 18, 2017 at 11:02 AM

For CanvasRenderer you can use SetAlpha method, that worked for me fine.

hide:

 gameObject.GetComponent().SetAlpha(0f);

show:

 gameObject.GetComponent().SetAlpha(1f);

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 shadowpuppet · May 18, 2017 at 11:00 PM

perhaps toggle visibility is the best way to go but I just turn off the object. YourGameObject.SetActive (false); and then YourGameObject.SetActive (true); doing it this way can lead to errors if you have a script still active referencing that gameobject, Like if a I enter a triggerzone that would normally effect that now inactive gameobject. But i simply add to that script " if (YourGameObject.activeInHierarchy == true)" . full example below

 void OnTriggerEnter(Collider other) {
         if(other.tag == "Player")
         {
  if (YourGameObject.activeInHierarchy == true)
 {
 
             animation.Play ("whatever");
             
         }
     }
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
  • ‹
  • 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

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

Prefab (Project Folder) to a GameObject (on the Scene) 2 Answers

How can I access children object from Tracked Image Prefab in ARFoundation? 0 Answers

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

How to create and save a gameobject to a prefab with a script 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