Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5: How to pass multiple parameters on button click function from inspector?

I want to send multiple parameter on button click from inspector?

Unity isn't allowing that to do, how can I achieve that? is there any other way? There are dozens of buttons and I am using same method for all buttons but sending different parameters, now I need to send multiple types of parameters. Can't figure out how?

like image 273
Affan Shahab Avatar asked Jul 22 '16 05:07

Affan Shahab


1 Answers

Calling Unity function from the Editor has limitation. It can only take one parameter and the function must be a non static function.

This is why it is important to know how to subscribe to events from code. Below is an example of a function that gets called when a Button is clicked. It passes in a string and an integer to that function. Simple, drag the Button to the some Button slot in the Editor and this should work.

public Button someButton;

void OnEnable()
{
    //Register Button Events
    someButton.onClick.AddListener(() => buttonCallBack("Hello Affan", 88));
}

private void buttonCallBack(string myStringValue, int myIntValue)
{
    Debug.Log("Button Clicked. Received string: " + myStringValue + " with int: " + myIntValue);
}

void OnDisable()
{
    //Un-Register Button Events
    someButton.onClick.RemoveAllListeners();
}
like image 112
Programmer Avatar answered Nov 11 '22 11:11

Programmer