is it possible to have two on click events in one button
good day i am trying to have two onclick events on one button. the first code would add the input fields to a google form but i want the other event to change scenes. This is what i have thus far
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class sendtogoogle : MonoBehaviour {
public GameObject name;
public GameObject contactnumber;
public GameObject time;
public GameObject seating;
public GameObject email;
private string Name;
private string ContactNumber;
private string Time;
private string Seating;
private string Email;
[SerializeField]
private string BASE_URL = "https://docs.google.com/forms/d/e/1FAIpQLSeDQ2b7jBRRd8wsmLSOJY9vdhqLbMmBraVFi5FnzM__p3OBsg/formResponse";
IEnumerator Post (string name, string contactnumber, string time, string seating, string email){
WWWForm form = new WWWForm();
form.AddField ("entry.147709443",name);
form.AddField ("entry.1096821271",contactnumber);
form.AddField ("entry.98853131",time);
form.AddField ("entry.1504298736",seating);
form.AddField ("entry.1983788742",email);
byte[] rawData = form.data;
WWW www = new WWW(BASE_URL, rawData);
yield return www;
}
public void Send() {
Name = name.GetComponent<InputField> ().text;
ContactNumber = contactnumber.GetComponent<InputField> ().text;
Time = time.GetComponent<InputField> ().text;
Seating = seating.GetComponent<InputField> ().text;
Email = email.GetComponent<InputField> ().text;
StartCoroutine (Post (Name, ContactNumber, Time, Seating, Email));
}
}
Answer by Vicarian · May 11, 2018 at 03:08 PM
You can add multiple listeners to a button in code:
void Start() {
someUIButtonReference.onClick.AddListener(SomeFunction1);
someUIButtonReference.onClick.AddListener(SomeFunction2);
}
void SomeFunction1() {
Debug.Log("SomeFunction1");
}
void SomeFunction2() {
Debug.Log("SomeFunction2");
}
or you can assign functions for a button click event to process in the editor.
The catch here is that the functions need to be publicly accessible. If you don't want a bunch of public functions, use the code method. The functions are called in the order that they're listed, so in this case SomeFunction1 will precede SomeFunction2.