- Home /
Canvas Button OnClick() function arguments
When you make a ui button, and you want to add an event to it, you add a script to a gameobject, and you define the gameobject and use the script. But what if you need 2 function arguments? I need to define 2 things, in the same function, but when I try to find the function in the button event list, I doesn't show up, and I can't use it. Any idea how to have 2 function arguments?
Answer by Mmmpies · Jan 27, 2015 at 04:04 PM
Just do the two things in the one function in your script.
EDIT
Sorry that was a bit brief, the point is if you do an OnClick well it can only be clicked in one way so you have to put all your actions in the function that's called when OnClick happens.
If you want to something like have the button get switched on one one click and the switch off on the next one just make a bool called AmIOn and set it to be the opposite of itself every click.
private bool AmIOn;
public void BeenClicked()
{
AmIOn = !AmIOn;
}
you can then check that bool in Update and do whatever actins you want if the buttom is on, e.g. rotate object.
Thank you for you're answer, but the problem is...
I want to have something like this:
public Canvas canvas; //The main canvas, of which all the menus are gonna be stored in
public void $$anonymous$$enuSwap(string name, string thisName){
canvas.Find(name).GetComponent<Canvas>().enabled = false; //Find the main canvas, and then find a child-canvas with a name.
canvas.Find(thisName).GetComponent<Canvas>().enabled = false; //Disable the menu which is currently enabled.
}
O$$anonymous$$ that makes more sense, pass it all as one string and use Split to separate the values out.
public void BeenClicked(string $$anonymous$$yString)
{
string[] stringArray = $$anonymous$$yString.Split(':');
Where you pass it "FirstName:SecondName" as a single string.
Doouble check those commands though as I don't have access to Unity to test so just doing it from memory. Hopefully you should have enough to work out how to do it. I used a : but it could be anything like '\n' for a newline character.
How does it know where to split it? :) Would it be like, first capital after beginning? :?
EDIT
Never$$anonymous$$d, I facepalmed... ;) Split by type, Which is ":"
:-) I think if you leave it empty it defaults to space if thats easier for you. I'll have access to unity soon so if you get stuck just let me know.
Your answer
Follow this Question
Related Questions
Only Rotate UI HUD on orientation change. 0 Answers
How do I obtain children on UI canvas text 0 Answers
How to hide Canvas? 1 Answer
Trying to place an UI Canvas Image next to another UI Image 1 Answer
Multiple Cars not working 1 Answer