Brendon
Brendon

Reputation: 103

Invoke not being called

I'm trying to delay my scene loading just a bit to let my button sound play through before the new scene gets loaded, but I seem to be having errors, which is weird considering the exact same code worked for my other button. The Debug below does not get called. Thank you!

public class DeathScene : MonoBehaviour {
    public void PlayAgain()
    {
        Invoke("Restart", 0.25f);
    }
    void Restart()
    {
        Debug.Log("restart");
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Upvotes: 1

Views: 341

Answers (2)

Ron Tang
Ron Tang

Reputation: 1622

  1. Checking Time.scale is not zero.
  2. Checking gameObject is not destoryed.
  3. Checking return value of IsInvoking is true.

If all no problem but Invoke still not work,it maybe bug of Invoke,use StartCoroutine.

Upvotes: 3

Bagdan Imr
Bagdan Imr

Reputation: 1286

Invoke doesn't work for a destroyed GameObject. I suppose your GameObject gets destroyed before PlayAgain is fired. Check it via OnDestroy

void OnDestroy () {
    Debug.Log("Oops");
}

Upvotes: 1

Related Questions