- Home /
onclick.addlistener only works once
I have a bunch of instantiated UI buttons in my canvas. I am trying to Find one of the buttons and attach onclick.Add listener to it. I am able to attach the listener with a coroutine, but it only works.
I am using a co routine instead of adding onclick.addlistener in start() functions because, my instantiated buttons are spawned later.
Below is the script I am using for it.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Product_Instance : MonoBehaviour {
public Button button;
IEnumerator butt () {
button = GameObject.FindGameObjectWithTag ("Cisterns").GetComponent<Button> ();
GameObject open = GameObject.FindGameObjectWithTag ("Open_Vertical");
button.onClick.AddListener(() => {
open.GetComponent<EasyTween_Product_button> ().OpenCloseObjectAnimation ();
});
yield return new WaitForSeconds(0.5f);
}
// Update is called once per frame
void Update () {
button = GameObject.FindGameObjectWithTag ("Special Series").GetComponent<Button> ();
if(button !=null){
StartCoroutine ("butt");
}
}
}
Comment