Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
7
Question by Hamacam · Dec 09, 2014 at 08:46 AM · uicanvasmenupause4.6

How can I get a UI Canvas to hide/appear on 'esc' button press?

I've been tackling with this script for a while and I don't have much understanding of C# so that probably doesn't help...

Still, this is the script I have so far (Has undergone MANY changes since the original, which made more sense but still didn't work, so please excuse any painfully obvious problems with the script.)

 using UnityEngine;
 using System.Collections;
 
 public class MenuAppearScript : MonoBehaviour
 {
 
     
 
     void Update()
     {
        
         if (Input.GetKeyDown("escape"))
         {
             GameObject.MenuCanvas = SetActive(true) ;
             }
         }
     }


Also, I'm using the Canvas that comes with 4.6.

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

4 Replies

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

Answer by Kiwasi · Dec 09, 2014 at 09:42 AM

Put this script on some other GameObject in the scene. I like to use an input manager or the canvas.

 using UnityEngine;
 using System.Collections;
  
 public class MenuAppearScript : MonoBehaviour {
  
     public GameObject menu; // Assign in inspector
     private bool isShowing;
 
     void Update() {
         if (Input.GetKeyDown("escape")) {
             isShowing = !isShowing;
             menu.SetActive(isShowing);
         }
     }
 }



Comment
Add comment · Show 11 · 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 Hamacam · Dec 09, 2014 at 09:52 AM 0
Share

Thank you, that worked perfectly!

avatar image Kiwasi · Dec 09, 2014 at 09:54 AM 1
Share

Hit the tick. And check out my profile for a link to some UI tutorials.

avatar image Hamacam · Dec 09, 2014 at 10:21 AM 0
Share

Sorry about this, may have gotten a bit over-excited about that fact that it worked, it's taking 3 button presses for the menu to actually show up. How can I fix this?

avatar image Hamacam · Dec 09, 2014 at 11:50 AM 0
Share

Now I've added a couple of Time.timescale's to it it now takes 5 button presses to appear...

avatar image Kiwasi · Dec 09, 2014 at 06:26 PM 0
Share

That's odd. As written it should toggle the menu every time you press escape. Try putting a debug.log inside the if statement and check if it is firing every time you press escape.

Show more comments
avatar image
7

Answer by jhsu · Jul 13, 2015 at 01:41 PM

I know this is an old question, but it kept coming up in my search and I found a much better solution that I will share for reference.

This solution is in C# and the script needs to be attached to the canvas GameObject itself. It uses .enabled which is essentially ticking on and off the checkbox next to the name in the inspector panel.

MenuToggle.cs:

 using UnityEngine;
 using System.Collections;
 
 public class MenuToggle : MonoBehaviour {
     private Canvas CanvasObject; // Assign in inspector
 
     void Start()
     {
         CanvasObject = GetComponent<Canvas> ();
     }
 
     void Update() 
     {
         if (Input.GetKeyUp(KeyCode.Escape)) 
         {
             CanvasObject.enabled = !CanvasObject.enabled;
         }
     }
 }

With .SetActive I was having to press the key multiple times as the OP mentioned in the comments to the other solution.

Comment
Add comment · Show 5 · 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 m_truly · Jan 30, 2016 at 06:25 PM 0
Share

Thank you jhsu! This worked great.

What would be the script to make a UI button toggle the canvas?

Thanks!

avatar image flowery m_truly · Feb 09, 2016 at 06:05 AM 0
Share

Hey, have you found a solution to your problem? Could you share? Thanks.

avatar image littleredcloud · Mar 22, 2016 at 12:58 PM 0
Share

Hey, this worked a treat for what I wanted, except like m_truly I would also like to trigger this with a button press from a button on another UI element (rather than the escape key).

I am working on it myself (mostly trial and error), but any feedback on how to do it would be most welcome! :)

avatar image Kramboja littleredcloud · Mar 22, 2016 at 01:03 PM 0
Share

create a public void which you call with the unity button and you can call the same void when pressing the escape button

avatar image littleredcloud · Mar 27, 2016 at 06:50 AM 1
Share

I found a way, using the following.

It has to be attached to the button, not to the canvas.

The Start section finds the relevant Canvas (YourNamedCanvas). The ToggleCanvas section does the toggling.

When you go to your button after you attach the script, drag the script on to the None (object) area, and then use the drop down menu to go to YourScriptName and select ToggleCanvas()

So, pressing your button in the game will toggle the canvas.

 {
     private Canvas CanvasObject; 
 
     void Start () 
     {
         CanvasObject = GameObject.Find("YourNamedCanvas").GetComponent<Canvas>();
         CanvasObject.GetComponent<Canvas> ().enabled = false;
     }
 
     public void ToggleCanvas ()
     {
         if (CanvasObject.enabled == true) {
             CanvasObject.GetComponent<Canvas> ().enabled = false;
         } else {
             CanvasObject.GetComponent<Canvas> ().enabled = true;
         } 
     }
avatar image
4

Answer by Mmmpies · Dec 09, 2014 at 08:52 AM

Add a CanvasGroup to the Canvas if not there already and then set the Alpha to 0 (for invisible) or 1 (for visible), Also set the Interactive and BlocksRaycast false (when invisible) and true if visible.

Note the Alpha is a float so you can fade in or out of being visible if you want.

see CanvasGroups

EDIT:

Easiest way is to create a public CanvasGroup so...

public CanvasGroup myCanvasGroup;

and reference that as myCanvasGroup.Alpha = x;

But you also need using reference at the top of the script:

using UnityEngine.UI;

Then in the inspector when the myCanvasGroup appears and is empty drag the canvas onto it.

I'm doing this in my head as I don't currently have access to Unity, it should work but may contain typos!

Good luck.

Comment
Add comment · Show 7 · 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 Hamacam · Dec 09, 2014 at 09:06 AM 0
Share

I altered my script to accommodate and yet still I don't really understand what I'm doing fully, how would I go about fixing the script for it?

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$enuAppearScript : $$anonymous$$onoBehaviour
 {
 
     public float alpha;
 
     void Update()
     {
        
         if (Input.Get$$anonymous$$eyDown("escape"))
         {
             CanvasGroup.alpha (1);
             CanvasGroup.Interactive(true);
             CanvasGroup.BlocksRaycast(true);
             }
         }
     }
avatar image SimonTS · Dec 09, 2014 at 09:24 AM 0
Share

You don't call the functions on the class but rather on the instance. You need a reference to the canvasGroup you want to enable

 public CanvasGroup canvasGroup;
 ...
 canvasGroup.alpha = 1;
 canvasGroup.interactable = true;
 canvasGroup.blocksRaycasts = true;

Then set this canvasGroup to the Gameobject with a CanvasGroup Component you wish to activate.

And as $$anonymous$$mmpies mentioned, you need to add

 using UnityEngine.UI

at the top of your script, so the computer knows what a CanvasGroup acutally is.

avatar image Kiwasi · Dec 09, 2014 at 09:34 AM 0
Share

Workable. But not needed.

avatar image Mmmpies · Dec 09, 2014 at 09:37 AM 0
Share

Not sure I understand Bored$$anonymous$$ormon, can you elaborate?

avatar image Hamacam · Dec 09, 2014 at 09:45 AM 0
Share

So, I changed my script again as told to:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class $$anonymous$$enuAppearScript : $$anonymous$$onoBehaviour
 {
 
     public float alpha;
     public CanvasGroup canvasGroup;
 
     void Update()
     {
        
         if (Input.Get$$anonymous$$eyDown("escape"))
         {
             CanvasGroup.alpha = 1;
             CanvasGroup.interactable = true;
             CanvasGroup.blocksRaycasts = true;
             }
         }
     }

But I'm getting 3 errors that all seem to be the same problem.

alt text

errors.jpg (46.5 kB)
Show more comments
avatar image
1

Answer by Kramboja · Dec 09, 2014 at 09:45 AM

I guess this should work.

//makes canvas visible by default

 private bool canvasOn = true;
 void Update()
 {
  if(Input.GetKeyDown("escape"))
  {
 //if canvas is visible you have to hide it
    if(canvasOn)
     canvasOn = false;
     canvas.enabled = false;
   }
    else
    {
     canvasOn = true;
     canvas.enabled = 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 Mmmpies · Dec 09, 2014 at 09:52 AM 0
Share

Hamacam, change CanvasGroup to canvasGroup in the if statement and careful with those braces/brackets looks like you might have too many }

avatar image Mmmpies · Dec 09, 2014 at 09:58 AM 0
Share

Actually the braces are probably O$$anonymous$$ just out of alignment.

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

10 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

Related Questions

Pause menu... Isn't pausing everything... 1 Answer

How to save players position then load a level (pause menu) 2 Answers

Unity Pause Menu Still Has Camera Movement! 1 Answer

Background color and structure disappear when enabling GUI Canvas 0 Answers

open a UI panel 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