- Home /
need my function to take 2 variables and show in OnClick list
I wrote a function for a set of buttons but after i wrote & built, it didn't show up in the OnClick function list on my buttons so i looked around on google and found out that you can't do this(no idea why though). I don't know how to work around this problem so i'm hoping one of you guys have a good idea.
In my game i have a lot of stuff that can be bought, each item has it's own panel and buy button which is supposed to open a different panel ment for all transactions("shop"), this panel includes 2 price texts, 1 for each currency. So a function that takes 2 variables so i can set it differently for each button in the editor seemed like an easy and effective way to do it.
here's the script:
public void OpenShop(int creditcost, int crystalcost){
CheckFunds();
creditsCostText.text = creditcost.ToString();
crystalsCostText.text = crystalcost.ToString();
if (ShipMenu.activeInHierarchy == true) {
panelOpened = false;
ShipMenu.SetActive(false);
} else {
WeaponMenu.SetActive(false);
}
Shop.SetActive(true);
}
Answer by Chmava · Apr 27, 2016 at 12:29 PM
Try this:
public void OpenShop (string item = "100,100")
{
string[] split = item.split (","[0]);
int creditcost = int.Parse(split[0]);
int crystalcost = int.Parse(split[1]);
and when you use it:
OpenShop ( CreditCost.toString() + "," + CrystalCost.toString() )
This might not be the best way around it. Unity really need to support multiple variables for UI Button onClick event.
Answer by TheSorm · Apr 27, 2016 at 10:26 AM
Just put the variables in an array of objects[] and give that array to the function
Answer by HarshadK · Apr 27, 2016 at 10:36 AM
There are a couple of options provided in this question: UI Button multiple parameters
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
HELP how to use buttons 1 Answer
Distribute terrain in zones 3 Answers
Trigger a Button Click 2 Answers
Rotating Tank Turret with correct angle 3 Answers