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 mdsmith98 · Feb 23, 2014 at 07:05 AM · activateinput.getkeyshowinghiding

Problem with toggling GameObject

Hello.

So I am trying to write a javascript file in which the user can press the F12 key to show/hide a GameObject. In this case, the GameObject starts out as being shown, however I would like to make it so when the player presses the F12 key once the GameObject won't be visible (or hidden), then when the GameObject is hidden, they should be able to press the F12 key again to show the object.

Here is my code:

ToggleGUI.js

 #pragma strict
 
 
 var show : boolean = true;
 var target : GameObject;
 
 function Update () {
 
     // Hides the GUI
     if(show == true){
         if(Input.GetKeyDown("f12")){
             target.SetActive(false);
             show = false;
         }
     }
     
     // Shows the GUI
     if(show == false){
         if(Input.GetKeyDown("f12")){
             target.SetActive(true);
             show = true;
         }
     }
 }


I was doing trial and error earlier and noticed some really odd things...

  1. If I change the second if under the comment // Shows the GUI, I set the Input.KeyDown to ("r") or some other random key, it will show the GameObject, and will work properly but not the way I wanted. I wanted it to be hidden then shown as many times as the player wants with the same key, in this case being the F12.

  2. If I delete the show=false; and the show=true; under the if's, the first if will happen, but the second won't and I know this is because the value of show is not being changed to make the second if happen... However with this code above with the changing of the variable show, neither if statement will run, not even the first one....

Yes, I have targeted my GameObject (called GUI Holder) through the inspector.

Is it even possible to show or hide a GameObject using a single key? Let me say this, I am very new to Unity and scripting, only been in unity about two weeks now, scripting for maybe a week if that, so please take it easy on me. If all possible, I would like code similar in nature to mine, or code that is easy to comprehend.

Please help!!

-Matthew

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
2
Best Answer

Answer by mattyman174 · Feb 23, 2014 at 07:15 AM

         if(Input.GetKeyDown(KeyCode.F12))
         {
             target.SetActive(show);
             show = !show;  // This reverses its current state
         }

Theres no need to check if "show" is true or false to begin with either.

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 mdsmith98 · Feb 23, 2014 at 07:18 AM 0
Share

Wow... I'm so new to this... Thank you so much! Worked beautifully like a charm. Greatly appreciated.

avatar image buttmatrix · Jun 01, 2015 at 04:59 AM 0
Share

Thank you so much!

avatar image
1

Answer by highpockets · Feb 23, 2014 at 07:21 AM

You are setting show to false and right afterwards, you are checking if it is false and since the key was down on that frame, it will set show back to true all in the same update..

What you need to do is:

 ToggleGUI.js
 
 #pragma strict
  
  
 var show : boolean = true;
 var target : GameObject;
  
 function Update ()
 {
 
 if(Input.GetKeyDown("f12"))
 {
 if( show == true )
 {
 target.SetActive(false);
 show = false;
 }
 else
 {
 target.SetActive(true);
 show = true;
 }
 }
 }
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 mdsmith98 · Feb 23, 2014 at 07:25 AM 0
Share

Your code worked perfectly as well, but mattyman174 answered first, and I used his code before I saw your answer. Thanks though for your answer! I'll keep it in $$anonymous$$d in case I have a similar issue where your code would work better. So thanks to you as well!

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make showing and hiding outline? 3 Answers

Hiding/Showing window on button 3 Answers

Distinguish whether my Unity (Mac) is activated as Pro version or still in 30-day trial? 2 Answers

Enter Trigger activate Gui Texture 1 Answer

how do i cycle back and forth through cameras in game. 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