Invoke not working on second scene load

Hello!

At the beginning of my scene, I use Invoke to delay adding the “Player” object until an animation is complete.

This seems to work fine, but if I unload the scene with the Player object and then go back in, the Invoke call doesn’t seem to work.

Here’s the particular code:

var playerPrefab : GameObject;


function Start () {
	Invoke("addPlayer", 1);
	
}

function addPlayer(){
	
	var cam = GameObject.FindWithTag("MainCamera");
	var newplayer = Instantiate(playerPrefab, transform.position, transform.rotation);
	cam.SendMessage("setTarget", newplayer);
	Destroy(gameObject);
}

I also have tried using “Awake” instead of “Start”

IsInvoking(“addPlayer”) returns true on all times… both when it works and when it doesn’t work.

Am I not using Invoke correctly? What would be a better way of doing this?

I am stupid! I just realized that the way I’m “unloading” my game is in the Pause menu. When the pause menu is open, Time.timeScale is set to 0. I never fix that (though I guess I assumed that it would reset when I load a new scene… it does not).

Setting Time.timeScale back to 1 when I quit to the menu fixes things.

-Sam

6 Likes

Oh damn it! I ran into the same problem. Thank you for posting your solution to this! :smile:

I have a very similar problem: if I reload a scene from my pause menu, all the InvokeRepeating commands I already called won’t stop. Setting Time.deltaTime = 1 doesn’t work either…

Thank you sooo much! I never would have found that out. You seriously made my day. Exact same issue. Thanks for the solution!!

Haha, same issue here, that was a tricky one! Thanks!

I had the same problem with coroutine. Thanks!

Hello, i have the same problem, i still have Time.timeScale = 1

Any help?

Haha, thank you for pointing our little mistake :smile:

You are my saviour.

shit, I had the same problem. Now I am stupid too.