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
0
Question by JustRoss · Oct 26, 2015 at 11:56 AM · inputcanvastogglekeykey pressed

Toggling with a key not working

Hey everyone, I've written a script that should toggle the UI, unfortunately I have no idea why it is not working. It works when toggling from off to on, but not on to off.

 public class intro : MonoBehaviour {
     public bool controls;
     CanvasGroup canvas;
     // Use this for initialization
     void Start () {
     canvas = GetComponent<CanvasGroup>();
     controls = true;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.I) && controls == true)
         {
             controls = false;
         }
         if (Input.GetKeyDown(KeyCode.I) && controls == false)
         {
             controls = true;
         }  
         if (controls == true)
         {
             canvas.alpha = 1;
         }
         if (controls == false)
         {
             canvas.alpha = 0;
         }
     }
 }
 
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
0
Best Answer

Answer by saschandroid · Oct 26, 2015 at 12:48 PM

 else if (Input.GetKeyDown(KeyCode.I) && controls == false)
 {
      controls = true;
 }

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 JustRoss · Oct 26, 2015 at 02:36 PM 0
Share

Ill check if that fixes it, but I don't understand why that should work, and why $$anonymous$$e doesn't.

avatar image saschandroid JustRoss · Oct 26, 2015 at 02:53 PM 0
Share

In the same frame (Update function) you set controls to false in the first 'if', but the second 'if' is called too (because controls is now false and Input.Get$$anonymous$$eyDown() still is true - same frame) and therefor controls is set back to true. That means the controls variable can never be false at the end of the frame and alpha will never be 0.

avatar image
0

Answer by Styla · Oct 26, 2015 at 03:06 PM

Hi,

Not pretty sure about my answer but I think the problem is that you put your key directly in the void update function.

So when you toggle from off to on , there is no problem because controls == false and it fires normally.

From the other side, it will read the code completly.

  if (Input.GetKeyDown(KeyCode.I) && controls == true)
          {
              controls = false;
          }
          if (Input.GetKeyDown(KeyCode.I) && controls == false)
          {
              controls = true;
          }  

If I recap, the input "I" will be down so controls will be true so it will put controls to false. Just after the input will be still down ( same frame so the instruction will also fire ) so it will enter the if (Input.GetKeyDown(KeyCode.I) && controls == false) so controls will be true again.

Try to put your if on the other side and you will know how to correct your mistake.

Best regards,

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Key Input for new Input System Limited? 1 Answer

Is there a way to get precise input ? 1 Answer

No keyboard events detected after any key is held. 0 Answers

Input Key named: LeftAlt is unknown 2 Answers

Check GUI toggle button with the M keyboar Key 2 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