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
3
Question by LadyAth · Sep 06, 2014 at 04:08 PM · uimenuunity 4.6

Unity 4.6 - How to hide a menu button in C#?

I have a menu with a button called btnNewGame. I have attached a C# script called MainMenu.cs to my camera and would like to execute a function that checks if there is an existing PlayerPrefs entry called "EXISTINGGAME" and if so, hide the menu button object, btnNewGame. The opposite logic would apply for btnLoadGame (but not there yet).

My question is, how do I set the new UI button to be inactive if the following function is called:

 public void CheckifGameExists(){
    if(PlayerPrefs.HasKey("EXISTINGGAME")) { 
       if(PlayerPrefs.GetString("EXISTINGGAME") == "Yes"){
          Debug.Log("This is an existing player");
         //hide new game button???
       }else if(PlayerPrefs.GetString("EXISTINGGAME") != "Yes"){
          Debug.Log("This is new player");
         //hide load game button???
       }
    }
 }

  

I am guessing I need to call CheckifGameExists in void Awake, or is there a better way?

Thanks!

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

5 Replies

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

Answer by HarshadK · Sep 06, 2014 at 05:55 PM

You can get the reference to your specific button game object (like btnNewGame) and set it to active or deactivate it using GameObject.SetActive(bool)

In the end all your UI elements from 4.6 are game objects itself with specific UI element related components Attached to them.

Comment
Add comment · Show 3 · 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 LadyAth · Sep 06, 2014 at 06:33 PM 0
Share

Ah thanks! Was not aware I could do a straight-up gameobject reference to it :)

avatar image Lokiare · Nov 18, 2014 at 08:06 AM 1
Share

I used TextVarName.GameObject.SetActive(bool). Since GameObject is the parent object of the UI element.

avatar image Blyler · Dec 04, 2015 at 08:38 PM 0
Share

This is the correct answer to the question. Great answer!

avatar image
7

Answer by RiceWilly · Sep 20, 2015 at 08:30 PM

I tried using @MaxM answer but it kinda messed the colour up of my buttons. In Unity 5 I got this working and it works perfect.

To Enable:

ButtonName.gameObject.SetActive(true);

To Disable:

ButtonName.gameObject.SetActive(false);

Hope it helps!

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

Answer by MaxM · Jul 03, 2015 at 10:23 PM

I am using Unity 5 and am using the following solution:

 public static void Hidden(Button but, bool isHidden)
 {
     if(isHidden)
     {
         but.enabled = false;
         but.GetComponentInChildren<CanvasRenderer>().SetAlpha(0);
         but.GetComponentInChildren<Text>().color = Color.clear;
     }
     else
     {
         but.enabled = true;
         but.GetComponentInChildren<CanvasRenderer>().SetAlpha(1);
         but.GetComponentInChildren<Text>().color = Color.black;
     }
 }
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 LadyAth · Jul 04, 2015 at 05:29 AM 0
Share

Thank you, $$anonymous$$ax$$anonymous$$ :)

avatar image
0

Answer by TD10074405 · Sep 06, 2014 at 05:04 PM

Where is "Yes" declared? Should that be = true / = false ?

This should work BTW

 public bool ShowLoad;
 public bool ShowNew;
     
 public void Awake(){
     ShowLoad = false;
     ShowNew  = false;
     
     if(PlayerPrefs.HasKey("EXISTINGGAME")) { 
         if(PlayerPrefs.HasKey("EXISTINGGAME") == yes) { 
             ShowLoad = true;
             ShowNew  = false;
         }else if(PlayerPrefs.HasKey("EXISTINGGAME") != yes) { 
             ShowLoad = false;
             ShowNew  = true;
         }
     }
 }
 
 public void OnGUI(){
     if(ShowLoad){
         if(GUI.Button(new Rect(50,0,100,50), "Load Game")){
             //Do the Load game dance
         }
     }
     if(ShowNew){
         if(GUI.Button(new Rect(50,50,100,50), "New Game")){
             //Do the New Game dance.
         }
     }
 }
Comment
Add comment · Show 4 · 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 LadyAth · Sep 06, 2014 at 05:50 PM 0
Share

I was under the impression OnGUI does not work with the new 4.6 UI system?

avatar image LadyAth · Sep 06, 2014 at 05:51 PM 0
Share

The 'yes' is a string I set in playerprefs after a character was selected.

avatar image TD10074405 · Sep 06, 2014 at 11:25 PM 0
Share

I was not completely aware of the new UI systems features and functions, I've not yet tried out the 4.6 Beta myself.. in that case i'll take alook into the documentation for it if its available.

Have you tested it though? I can't see them removing a whole chunk of functionality that most people know and use, maybe Deprecate it but can't see them removing it all together...

avatar image LadyAth · Sep 07, 2014 at 10:01 AM 0
Share

OnGUI is not used with the new UI. It works the similar to NGUI from a visual canvas that call public functions directly from scripts. This is why I was looking for a way to reference a button object directly.

The new UI system takes a bit of getting used to, but I find it a lot less complicated than NGUI and quite intuitive once you get your head around the different way of working.

avatar image
0

Answer by lolmaster2 · Jan 26, 2016 at 12:23 PM

This works for Unity 5

 // Hide
 GameObject.Find("InstructionsButton").GetComponent<Button>().enabled = false;
 GameObject.Find("InstructionsButton").transform.localScale = new Vector3(0,0,0);

 // Show
 GameObject.Find("InstructionsButton").GetComponent<Button>().enabled = true;
 GameObject.Find("InstructionsButton").transform.localScale = new Vector3(1,1,1);
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Canvas Button OnClik() 1 Answer

Side scrolling menu 0 Answers

Unity 4.6 UI Lags on android 0 Answers

Multiple UI menus, but arrow keys only work on the first one 0 Answers

Create Dynamic buttons with info and update in new Unity 4.6 UI 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