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 harry12 · Nov 08, 2014 at 06:58 AM · buttonrenderertogglehide

How to toggle object visibility with keyboard button?

Hi, ive been searching for hours for a solution to this but everyone seems to say something different... and none have worked for me.

All i want to do is press a single button to toggle the visibility of an object. Not sure if it makes a difference, but the objects in question is under an empty gameobject parent (as ideally there would be multiple objects all being turned off via control of the parent).

Ive tried both with the parent and just targeting the object itself but obviously neither works. Ive tried so many different codes and variations that theres no point in posting them all here really. Id just like to know what particular methods work for you when essentially turning on and off an objects visibility. Full code please, im a noob and have no idea how to fill in the blanks :P

Thanks for any help/suggestions - they are desperately needed and greatly appreciated! :D

Comment
Add comment · Show 3
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 Linus · Nov 08, 2014 at 06:36 AM 0
Share

http://docs.unity3d.com/ScriptReference/Renderer-enabled.html http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$eyUp.html

If you have problems putting it together try some basics tuturials.

Show us what you got so far, also are you using C# or UnityScript(also called JavaScript even though it not JavaScript) Editet title, question speeks for itself

avatar image harry12 · Nov 08, 2014 at 06:49 AM 0
Share

Yeah im using java/unityscript, and this was the closest ive come - but i feel like im completely on the wrong track:

 function Start () {
 
 }
 
 function Update () {
     if (Input.Get$$anonymous$$eyDown ("space")) {
          ToggleVisibility(); 
          } 
 }
 
 function ToggleVisibility() { var renderers = gameObject.GetComponentsInChildren.(); 
     for (var r : Renderer in renderers) { 
         r.enabled = !r.enabled; 
     } 
 }

it seems like it should be really straightforward? A few hours ago I thought it wouldn't be much more difficult than scripting the unchecking of the visibility boxes in the inspector hahaha...

avatar image harry12 · Nov 08, 2014 at 06:57 AM 0
Share

Ive also tried this:

 function Update () {
     if (Input.Get$$anonymous$$eyDown ("space")) {
          if (SnowScene.renderer.enabled = true) {
              SnowScene.renderer.enabled = false;
          }
     }
     else {
         SnowScene.renderer.enabled = true;
     }
 }

where "SnowScene" is the empty gameobject parenting the actual objects. What am i missing?

1 Reply

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

Answer by Linus · Nov 08, 2014 at 07:21 AM

This should do it

 #pragma strict
 
 var isVisible : boolean = true; //Start as on. 
 
 function Start () {
     
 }
 
 function Update () {
     if (Input.GetKeyDown("space")) {
         
         
         if(isVisible == true){
             ToggleVisibility(false); //Tell toggle inside ToggleVisibility to be false
             isVisible = false;
         } else {
             ToggleVisibility(true);
             isVisible = true;
         }
     }
     
 }
 
 function ToggleVisibility(toggle : boolean) { 
     var renderers : Renderer[] = gameObject.GetComponentsInChildren.<Renderer>();
     for (var r : Renderer in renderers) {
         if(toggle == true){
             r.enabled = true;
         } else {
             //Toggle is not true so must be false
             r.enabled = 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 Linus · Nov 08, 2014 at 07:25 AM 0
Share

I guess you also could do:

 if(r.enabled){
 r.enabled = false;
 } else {
 r.enabled = true;
 }  

Ins$$anonymous$$d of having the toggle and isVisible variables

avatar image harry12 · Nov 08, 2014 at 07:34 AM 0
Share

OH $$anonymous$$Y GOD! Thankyou you are a LEGEND!!! Looks like my mixed media final assessment is going to be handed in after all!! :D cannot thank you enough, that did it perfectly! :D

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

GUISkin & Scripting 2 Answers

How do I make a function not appear on UnityEvents / Button OnClick lists? 0 Answers

GameObject as a toggle button to play character Animation 1 Answer

GUI.Toggle to only send one action, not repeated 1 Answer

Enable/Disable JS Script Problem (AR) using CS 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