Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Invoke not working on second scene load

Discussion in 'Editor & General Support' started by TheSambassador, Feb 17, 2011.

  1. TheSambassador

    TheSambassador

    Joined:
    Jan 1, 2011
    Posts:
    31
    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:
    Code (csharp):
    1. var playerPrefab : GameObject;
    2.  
    3.  
    4. function Start () {
    5.     Invoke("addPlayer", 1);
    6.    
    7. }
    8.  
    9. function addPlayer(){
    10.    
    11.     var cam = GameObject.FindWithTag("MainCamera");
    12.     var newplayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    13.     cam.SendMessage("setTarget", newplayer);
    14.     Destroy(gameObject);
    15. }
    16.  
    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?
     
  2. TheSambassador

    TheSambassador

    Joined:
    Jan 1, 2011
    Posts:
    31
    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
     
  3. Ches81

    Ches81

    Joined:
    Nov 27, 2012
    Posts:
    7
    Oh damn it! I ran into the same problem. Thank you for posting your solution to this! :D
     
  4. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    305
    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...
     
  5. SniperEvan

    SniperEvan

    Joined:
    Mar 3, 2013
    Posts:
    161
    Thank you sooo much! I never would have found that out. You seriously made my day. Exact same issue. Thanks for the solution!!
     
  6. El_boy

    El_boy

    Joined:
    May 23, 2013
    Posts:
    9
    Haha, same issue here, that was a tricky one! Thanks!
     
  7. adriono

    adriono

    Joined:
    Oct 25, 2015
    Posts:
    1
    I had the same problem with coroutine. Thanks!
     
  8. moh05

    moh05

    Joined:
    Nov 26, 2015
    Posts:
    65
    Hello, i have the same problem, i still have Time.timeScale = 1

    Any help?
     
  9. bakar123

    bakar123

    Joined:
    Oct 9, 2019
    Posts:
    1
    Haha, thank you for pointing our little mistake :D
     
  10. abhi1abhisheksaha

    abhi1abhisheksaha

    Joined:
    Nov 9, 2021
    Posts:
    1
    You are my saviour.