- Home /
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!
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.
Ah thanks! Was not aware I could do a straight-up gameobject reference to it :)
I used TextVarName.GameObject.SetActive(bool). Since GameObject is the parent object of the UI element.
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;
}
}
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.
}
}
}
I was under the impression OnGUI does not work with the new 4.6 UI system?
The 'yes' is a string I set in playerprefs after a character was selected.
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...
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.
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);
Your answer
Follow this Question
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