- Home /
Canvas Button OnClik()
I have made a couple of menus in the same scene. I wan't, when I press a button, I open up another menu. I kinda know how to do that.
But I have a question.
In the "Menu Manager" script, where I define the functions the buttons are going to use, can you not do this:
public void MenuSwap(**string name, Canvas thisCanvas**){
//Do whatever
}
can you not have 2 variables in the brackets??
You can and it works. They are called Function Arguments.
I don't understand why this gives you issues.
When I put 2 "function arguments" in, the function/ void disappears from the functions in the button list. I can't find it! :c
But of course, this is when I use the canvas button, OnClick(); Not when I use it normally.
Answer by Kiwasi · Jan 27, 2015 at 08:16 AM
You can't have two variables. You can wrap both variables in a single UnityObject and pass that in.
Or you can add the listeners via script. You can add more variables that way.
Can you give an example in script? Because I didn't understand much of that?
What I think he means:
Option 1:
public void $$anonymous$$enuSwap(Object data){
//Do whatever
}
And then you can have an object on your scene or a prefab with the data you want to pass. You reference this object as the paremeter on the onClick listener and then cast it inside your function to extract the data
Option 2:
//Anywhere inside your code
button.onClick.AddListener(() => $$anonymous$$enuSwap("$$anonymous$$y String", canvasReference));
I have only used AddListener for functions with no parameters, but maybe it could work like this. Don't forget to remove listeners later if you wont need them.
The problem with option 1 is, that I have more than 2 menus. And if I understood this correctly, I would have to have many objects or prefabs with my data... And I want a script with which I can use for every button, without having to change much. Like maybe just a name to search for. (the next menu.) But Then I would have to disable the first canvas, and enable the next. But I can't define the buttons parent canvas.
And I don't understand option 2. ;)
Just add a script to the button with a few public variables holding the data. Pass that into your event trigger.
I tried this, and it seems like a lot of work, is there no other way to do it?
Your answer
Follow this Question
Related Questions
Unity 4.6 UI Lags on android 0 Answers
UI doesn't render after scene reload. 0 Answers
Scaling a menu bar 1 Answer
Basic question about creating a menu 1 Answer
UI menu not rendering properly 1 Answer