Coroutine Not Completely executing

I’m been stuck on this silly bug for some time now and I can’t find an answer why it may have happened. I’m trying to do a very simple which is deactivating a panel GameObject using a coroutine (to allow the button SFX to play). Heres what I’m trying to do:

public class ExampleScript : MonoBehaviour
{

 public GameObject confirmPanel;

public void CancelReturnToMenu()
{
StartCoroutine(CancelReturnToMenuTime());
}

public IEnumerator CancelReturnToMenuTime()
{
 yield return new WaitForSeconds (0.25f);
confirmPanel.SetActive(false);

}

}

I’m calling the function CancelReturnToMenu() on my button via an OnClick event in Unity.

I’m not sure why this is not working, I’m not doing anything else. The coroutine is waiting 0.25 sec because I can hear the sound effect, however I’m not sure why the code after that line is not executing.

Thanks for the help

This is by far the most common error i have seen in unity answers, there are 2 posibilitys, you are destroying the gameobject attached to the examplescript behaviour, or you are desactivating it

Hi, @Ryox92! Are you sure, you do not deactivate game object with ExampleScript, when you click button?
Maybe your button do 2 things - call func CancelReturnToMenu and deactivate game object with ExampleScript?