0

I am just starting with unity and having problems to show/hide menu panel with a button click.

I am using unity 5 and able to do it by playing On Click() button parameter right in the inspector:

I click "+", drag my panel in object field, and the select GameObject > SetActive(Bool) function.

However what I am looking to learn is the way to achieve similar behavior with C# script. I tried:

using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Events;
 using System.Collections;

 public class closebutton : MonoBehaviour {

     public GameObject menu;

     void OnMouseDown() {
         menu.SetActive(false);
     }

 }

but nothing happens...

Please help me to achieve this basic task :)

Acidon
  • 1,294
  • 4
  • 23
  • 44
  • The way you are already doing it is better (in inspector with onClick). It's okay to do this for learning but there might be better stuff to learn. – Reasurria Aug 13 '15 at 13:33

1 Answers1

2

The way you are already doing it is better (in inspector with onClick).

If you are just curious then you can do the following:

void Start()
{
    GetComponent<Button>().onClick.AddListener(() => {
                                                         menu.SetActive(false);
                                                     });
}
Reasurria
  • 1,808
  • 16
  • 14
  • Thanks, exactly what I was looking for! – Acidon Aug 13 '15 at 15:59
  • Sweet. Also, I think it works if you pass just a function name in there. Like AddListener(theGameObject.theFunction); That might be a bit closer to home if you are learning. – Reasurria Aug 14 '15 at 05:09