UI Button multiple parameters

Hi, assuming I’m using Unity 4.6 and the new GUI system; is there a way to assign multiple parameters to a function called during the OnClick() event of a button?

I mean through the inspector, becuase I managed a solution using this code:

btn.onClick.AddListener(() => { Login(username.text, password.text);  });

But I was wondering if there’s was a simple way (obtain the same result using only the inspector).

Thank you,


You can pass anything that inherits from Unity’s object class as a parameter for the OnClick events. So simply create an object that inherits from ScriptableObject or MonoBehaviour, add multiple fields to contain the required data, and pass that through your OnClick event in the inspector.

Disclaimer: I haven’t tried this yet, but according to the docs it should work.

You can pass multiple integers by passing the numbers in a string then use .Substring() and int.Parse to convert the string of numbers to individual integers. If you pass the string “277830”, the function below will convert the string to playerposition = 27, playerhitpoints = 78, and playerdamage = 30.

public void playerstats (string stats){ 
 string temp = stats.Substring(0,2);
 playerposition = int.Parse(temp);
 temp = stats.Substring(2,2);
 playerhitpoints = int.Parse(temp);
 temp = stats.Substring(4,2);
 playerdamage = int.Parse(temp);

Not without some kind of extension or plugin, i’m afraid.

What you can do tho, is have 2 functions, which actually just stores those parameters, then use a third one to use those parameters…

Here’s what I have in mind :

Have a script with 3 functions like so :

string username;
string password;

public void StoreUser(string _username)
  username = _username;

public void StorePassword(string _password)
   password = _password;

public void Login()
    //login with Username and password.

and in the button, have it call the three functions. one after the other.

I am aware, this is far from optimal, but it’s a workaround.

@marf @jokim because the “problem” is still the same - after years … “Avoid making all properties public, because it’s just a free for all and how spaghetti code is cooked up.” - to quote john stejskal about getters, setters and auto properties in C# - brought me to this solution:

make the username variable accessible in editor

public string username { private get; set; }  // not visible without these properties   

and assign string username in the OnClick() section of your button first. you may pass as many different types of variables to your function - step by step of course. … and at the end you call Login(string) passing the password as last parameter like

public void Login(string password)
      LoginFunction(username, password);

that’s it. less lines of code, less functions to call.

Hey, @marf … Hope I’m not too late

Just in case other developers are wondering how to use multiple arguments with Unity Event

Y’all can check this package I made

Events 2.0 for Unity


I think a much simpler version would be to just put those values in an array. That’s what I am doing for my login code and it works well.

I have two input fields, I create an array with length of 2 and put the values of my text in that. I then return that array and pass that in as my parameter when I send the info on button click. So you could do something like this. (Note: I didn’t test this exact bit so you might need to create an array variable and set the value to get user data like string arr = getUserData(), and pass that into the listener instead.

private string[] getUserData()
	InputField[] __inputFieldArr = GetComponentsInChildren<InputField>();
	string[] __userDataArr = new string[2];
	__userDataArr[0] = __inputFieldArr[0].text.text;
	__userDataArr[1] = __inputFieldArr[1].text.text;

	return __userDataArr;

btn.onClick.AddListener(() => { Login(getUserData());  });