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.

5
  • 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 ? Commented Nov 10, 2016 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. Commented Nov 10, 2016 at 11:52
  • Can you check if OnSelect is being called by putting Debug.Log inside it? Commented Nov 10, 2016 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. Commented Nov 11, 2016 at 9: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. Commented Nov 11, 2016 at 18:37

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

GREAT !! if you are new to unity, I really urge you to check out this QA ... stackoverflow.com/questions/36244660/… the Event System is the handiest thing in unity. you will use it all the time. be sure to vote up my explanation there! :-)
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?
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!
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.