Code Unreachable or Unable to Execute?

The code snippet below is on a script attached to a GameObject in the Canvas that has a Button on it. It populates that Button’s OnClick listener. The code is executed when the button is clicked. The problem is the final line of code never executes. The GameObject is never turned off. The IF statement that encapsulates that line does indeed return true (as I’ve tested it thoroughly through debugging). The method “TransferEnhancementToUnit” returns a boolean value obviously. Any ideas on why the GameObject can’t be turned off?

public void OnClick()
{
    if (Game.instance.selectedUnit != null)
    {
        if (Game.instance.localPlayer.TransferEnhancementToUnit(Game.instance.localPlayer.Enhancements[enhancementIndex], Game.instance.selectedUnit))
            gameObject.SetActive(false);
    }
}

I actually have no idea… but you can try this to eliminate any other factors:

    public void OnClick()
    {
        gameObject.SetActive(false);
    }

If this also doesn’t work then there might be something in the UI (or your scripts) which keeps the object alive. Maybe disabling it one frame later could trick it.

    public void OnClick()
    {
        this.StartCoroutine(OnClickRoutine());
    }

    IEnumerator OnClickRoutine()
    {
        yield return null;
        gameObject.SetActive(false);
    }