Invoke not working

Ive tested the code on start and update and it seems to work however once i put it in an OnTriggerEnter2D it seems to no longer work, i have other things on the OnTrigger and they work perfectly fine, its just the invoke.

// Start is called before the first frame update

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Activator")
    {
        canBePressed = true;
        BattleFisticonAnimator.SetBool("isCollided", true);
        Invoke("DestroyObject", 1.0f);
    }
}

private void OnTriggerExit2D(Collider2D other)
{

    if (other.tag == "Activator")
    {
        canBePressed = false;
    }
}

void DestroyObject()
{
    Destroy(gameObject);
}

}

CredibleBH
First suggestion- instead of using ‘GameObject.Find(“name”)’ to destroy your objects, just use ‘objectCollided.gameObject’. It’s much cleaner.

Second suggestion: Instead of using Invoke, use StartCoroutine, like so:

delegate void InvokedFunction();
IEnumerator WaitAndInvoke(float secondsToWait, InvokedFunction func) {
yield return new WaitForSeconds(secondsToWait);
func();
}

// Now, in your collision function where you want to
// make the delayed function call:
StartCoroutine(WaitAndInvoke(2, enemyRespawn));

Hi,
I tested your code and the Invoke workes for me, so maybe the problem is somewhere else. Did you make sure, your code gets into the if - section? You can check it, by setting the “canBePressed” Boolean to public and check in the Inspector, if the value changes, or just write a Debug.Log into this section. Or maybe you just forgot to set the tag on the colliding object, misspelled it etc.
_
I also tried to clean up your code a little bit. If you only want to destroy the object in the Invoke function, you can also use the Destroy function with an optional delay timer. If you ask for a tag, it is faster to use the CompareTag function.
_
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(“Activator”))
{
canBePressed = true;
BattleFisticonAnimator.SetBool(“isCollided”, true);

        Destroy(gameObject, 1);
    }
}
private void OnTriggerExit2D(Collider2D other)
{
    if (other.CompareTag("Activator"))
    {
        canBePressed = false;
    }
}