Using OnMouseDown (or similar) to toggle worldspace UI (4.6)

I’d been using the following c# script to make a level select menu appear when a door in the scene is clicked, and disappear when clicking anywhere else.

using UnityEngine;
using System.Collections;

public class Door_click : MonoBehaviour {

	private bool toggleBox = false;
	
	void OnMouseDown (){
		toggleBox = true;
		
		Debug.Log ("clicked on door!");
	}
	
	void OnGUI(){
		
		if (toggleBox)
			// Make a background box
			GUI.Box (new Rect (Screen.width/2, Screen.height/2 - 100, 200, 170), "Select room");
		
		
		if (toggleBox)
			// Make the first button.
		if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 - 70, 180, 25), "Room 1")) {
			Application.LoadLevel ("scene_1");
		}

		if (toggleBox)
			// Make the second button.
		if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 - 35, 180, 25), "Room 2")) {
			Application.LoadLevel ("scene_2");	
		}

		if (toggleBox)
			// Make the third button.
		if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2, 180, 25), "Room 3")) {
			Application.LoadLevel ("scene_3");	
		}

		if (toggleBox)
			// Make the fourth button.
		if (GUI.Button (new Rect (Screen.width/2 + 10, Screen.height/2 + 35, 180, 25), "Room 4")) {
			Application.LoadLevel ("scene_4");	
		}

		Event e = Event.current;
		if (e.type == EventType.MouseDown) {
			toggleBox = false;
		}
	}
}

I’m completely new to any type of programming, so it’s probably really messy, but at least it was working.

I’m now trying to do the same thing with the new UI, but I’m completely stuck.

How do I write a script, that can be attatched to my door, to make a different game object (in this case the canvas with all the buttons on) appear and disappear?

Just reference the base canvas and use SetActive.

using UnityEngine.UI;    // add this to the top

public Canvas myCanvas;  // drag teh canvas on here



void OnMouseDown()
{
    myCanvas.gameObject.SetActive(true);
}

You can also Add Component and give the base canvas a Canvas Group and hide the canvas using that if you don’t want to use SetActive.