Hi, using unity 5.6 -
I have three buttons that I am attempting to instantiate with listeners in C#. In my function TaskOnClick(), I want to be able to be able to tell which button has been clicked so I can add further functionality.
I am fairly sure I am mucking up some code in the Start() function and also not sure if I need to add a bool to the TaskOnClick() function or if I should just use IF or Switch statements to parse through the clicks. I have been following this example: https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Button02 : MonoBehaviour {
public Button[] buttons;
private void Start()
{
for (int i = 0; i < buttons.Length; i++)
{
Button btns = buttons*.GetComponent<Button>();*
btns.onClick.AddListener(TaskOnClick);
}
}
public void TaskOnClick()
{
// how can I add a boolean here to check which button has been clicked?
Debug.Log(“you have clicked this button”);
}
}
I am wondering if something along the lines of this in TaskOnClick() might work: (obviously this is very crude…) Thank you in advance.
public void TaskOnClick() {
for (int i= 0; i < Lenght; i++) {
if (buttons[0] == true)
‘do something’…
else if(buttons[1] == true)
‘do something else’