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!

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.

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!

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.
		}
	}
}

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);

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;
    }
}