- Home /
Is it possible to invoke a button Press With a Coroutine?
I have a series of coroutines that I would like to see if I can get them to invoke a button press but I have not found any information on if that is even possible or if so how?
Do you mean you want to mimic a button press visually?
Well we have UI button on a canvas panel that I'm wondering if we can invoke a press with a coroutine.
You want to visually show the button pressing though? Not just simply running the logic that is connected to the button press?
Answer by daleran · Sep 12, 2016 at 11:22 PM
So, if I understand your question correctly, you are trying to invoke the functionality of a button that is blocked by another panel some other way. All you have to do is get a reference to the button and invoke the onClick event that all buttons have:
UnityEngine.UI.Button myButton.onClick.Invoke();
It's all in the Unity documentation: UI.Button
Thanks @daleran, but will that work from a coroutine?
Yeah, a coroutine is just a function that gets called asynchronously in unity. It's not like a thread where you have to worry about thread safety. It's just unity's way of running things in parallel. Anything you can call from update() or OnCollision() or any other method can be called from a coroutine. $$anonymous$$ore info here.
O$$anonymous$$ awesome @daleran I'll have to try this. So all I should need to do if my button is called: "CloseButton"
in my coroutine I would do something like"
IEnumerator AbortSP$$anonymous$$screen(){
yield return new WaitForSeconds (0.5f); //Wait Time
UnityEngine.UI.Button CloseButton.onClick.Invoke();
}
Is that right?