Reputation: 29
How to pass the value by clicking UI button in unity
i have 4 buttons in my scene so what i want to do means
button A have 5 button B have 25 button C have 2 button D have 7
EXAMPLE: If i click the button A and Button B value 30
and how to detect the button is clicked first time and second time ??
Please Help me out thank you in advance
Upvotes: 0
Views: 3009
Reputation: 85
To pass a value in a UI button you can use a public function with one parameter.
Here's an example code using switch to detect which button was clicked, and a dictionary to count clicks for each button.
int sum;
Dictionary<string, int> clickCounts = new Dictionary<string, int>();
void Start()
{
clickCounts.Add("A", 0);
clickCounts.Add("B", 0);
clickCounts.Add("C", 0);
clickCounts.Add("D", 0);
}
public void ButtonClick(string buttonLetter)
{
switch (buttonLetter)
{
case "A":
sum += 5;
clickCounts["A"]++;
break;
case "B":
sum += 25;
clickCounts["B"]++;
break;
case "C":
sum += 2;
clickCounts["C"]++;
break;
case "D":
sum += 7;
clickCounts["D"]++;
break;
}
}
Upvotes: 1