- Home /
Why won't invoke work in my script to delay the scene change?
Begin Waiting gets printed to the console.
After Waiting does not.
I just want to make it so that an explosion animation will be played when the player is destroyed before the game goes to the GameOver Screen.
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player")
{
Instantiate(explosionAnimation, other.gameObject.transform.position, other.gameObject.transform.rotation);
other.gameObject.SetActive(false);
Destroy(this.gameObject);
Invoke("DelayedAction", 1f);
Debug.Log("BEGIN WAITING");
}
}
void DelayedAction()
{
Debug.Log("AFTER WAITING");
SceneManager.LoadScene("GameOver");
}
Answer by Sonicbulletwolf · Sep 07, 2018 at 07:32 PM
In your code you have Destory(this.gameObject) if you doing that it won't have time to finish the code. Thats why you're not seeing the after message
This is probably the case. @emilibroooo if your goal is to hide the (what looks to be an) explosion, disable the renderer. Unless you've explicitly told Unity to preserve that GameObject between scene loads, then it'll be destroyed regardless.
Thanks! I changed
destroy(this.gameObject);
to
this.gameObject.SetActive(false);
and it worked :)
Good to hear. If this solved you problem, then please mark it as the "Correct Answer" so that this question can be closed.