1

I have three buttons, when you tap one button, a panel with a text appear.

The problem is when I attached the script, nothing happens. The "click" is registered but the the panel never appears.

My script is attached to each of the button and is something like this:

public GameObject panel; //i use to put the panel in unity
bool selected = false;

void Start () {    
    panel.SetActive(false); 
}

void OnSelect() {    
    selected = !selected;
    panel.SetActive(true);
}

I probably have to do something else with the panel but I can't figure it out.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Angelica cruz
  • 81
  • 1
  • 11
  • How does the "OnSelect" function is called ? Have you added this callback on the "OnClick" listener of the button from the inspector ? Have you tried to add a Debug.Log to make sure the function is called ? Maybe your panel is too big ? – Hellium Nov 10 '16 at 11:44
  • hi! i'm trying the program with the hololens. i use a click sound to see if the select is action i being recognized and it works. it's the showing part of the panel and the text that doesn't work. take note that "text" is child of Panel and Panel itself is a child of canvas. – Angelica cruz Nov 10 '16 at 11:52
  • Can you check if `OnSelect` is being called by putting `Debug.Log` inside it? – Programmer Nov 10 '16 at 23:26
  • I tried putting a sphere instead of a Canvas/Panel and it works fine. It's making the Panel appear and disappear that give me hard time. – Angelica cruz Nov 11 '16 at 09:26
  • You can't use this system unless you use **ISelectHandler** and more, which is quite difficult for beginners. I strongly recommend you master the simpler technique I explain in my answer. it is very easy. – Fattie Nov 11 '16 at 18:37

2 Answers2

1

Do it like this:

(1) Add the canvas to your project

(2) BIG TIP - be sure to select Scale with screen size.

That's the only one you ever use. Unity accidentally set the wrong default there, they have not fixed it yet.

(3) In your Canvas, add a BUTTON Make it say perhaps "Test"

(3) In your Canvas, add another BUTTON Make it say perhaps "Another Test"

(4) Make a script something like this...

public class MainScreen:MonoBehaviour
    {
    public void UserClickedTest()
        {
        Debug.Log("test..");
        }
    public void UserClickedAnotherTest()
        {
        Debug.Log("another test..");
        }
    }

(5) put ONE copy of that script on ANY object you like. You can put it on your camera, on the canvas, or anywhere else that makes sense

For now let's say you put it on your CAMERA object, for example.

(6) Click on the button "Test" .....

enter image description here

And do this ...

  1. click the PLUS button under OnClick

  2. you see the slot that says "_main" in this example. DRAG your CAMERA item from HEIRARCHY, to that slot

  3. Using the drop down menu:

select the "UserClickedTest()" function ...

good eh?

  1. Now for the other button, do the same but select the "UserClickedAnotherTest()" function.

  2. You're done! Run and test!

You can't use the OnSelect system unless you use ISelectHandler and more stuff: it is difficult for beginners. I strongly recommend the OP masters the simpler technique I explain here. Enjoy!

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • GREAT !! if you are new to unity, I really urge you to check out this QA ... http://stackoverflow.com/questions/36244660/simple-event-system-in-unity the Event System is the handiest thing in unity. you will use it all the time. be sure to vote up my explanation there! :-) – Fattie Nov 11 '16 at 20:53
  • hey! just a quick newbz question. Since I cannot use OnSelect to click a button, do you think i can use a voice input method? – Angelica cruz Nov 14 '16 at 15:22
  • That's actually a great question, and I would encourage you to just post a quick new question about that. I'm afraid I don't know the answer offhand. Cheers! – Fattie Nov 14 '16 at 15:28
0

Maybe you attached the script to the panel. If so, your scripts can not be executed as long as your GameObject is SetActive(false).

I hope I was able to help you.

Dlyx
  • 97
  • 10
  • You can't use this system unless you use ISelectHandler and more, which is quite difficult for beginners. I strongly recommend the OP masters the simpler technique I explain in my answer. It is very easy. – Fattie Nov 11 '16 at 18:38