Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reloading scene causes FixedUpdate() to not execute

Discussion in 'Scripting' started by goldenmindcreations, Jan 8, 2018.

  1. goldenmindcreations

    goldenmindcreations

    Joined:
    Jun 10, 2017
    Posts:
    4
    Hello, I have a gameObject (the title, which is a sprite image) in my home scene that performs a move operation (which gives the title a shaky/energized effect), correctly via the following script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class TitleShaker : MonoBehaviour {
    8.  
    9.     private bool shakePositioning = false;
    10.     private Vector3 tempShakePosition;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         Debug.Log("got here");
    15.         shakePositioning = false;
    16.         transform.position = Vector3.zero;
    17.     }
    18.  
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.      
    23.     }
    24.  
    25.     private void FixedUpdate()
    26.     {
    27.         Debug.Log("got here");
    28.         if (!shakePositioning)
    29.         {
    30.             tempShakePosition = Vector3.zero + Random.insideUnitSphere * .025f;
    31.             shakePositioning = true;
    32.         }
    33.         else
    34.         {
    35.             if (transform.position != tempShakePosition)
    36.                 transform.position = Vector3.MoveTowards(transform.position, tempShakePosition, Time.deltaTime * 15.0f);
    37.             else shakePositioning = false;
    38.         }
    39.     }
    40.  
    41. }
    I then press a play button which causes a scene change to my gameplay scene via the following script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class PlayButton : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.     }
    19.  
    20.     public void PlayButtonClicked()
    21.     {
    22.         StartCoroutine(LoadGameScene());
    23.     }
    24.  
    25.  
    26.     IEnumerator LoadGameScene()
    27.     {
    28.         // The Application loads the Scene in the background at the same time as the current Scene.
    29.         //This is particularly good for creating loading screens. You could also load the Scene by build //number.
    30.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GameScene");
    31.  
    32.         //Wait until the last operation fully loads to return anything
    33.         while (!asyncLoad.isDone)
    34.         {
    35.             yield return null;
    36.         }
    37.     }
    38. }
    Once the game is over, I then call for a scene change to go back to the home scene via the following script (this is just an excerpt from a much bigger script - I don't feel the rest of the script is necessary for this issue):
    Code (CSharp):
    1.     public void GameOver(bool state)
    2.     {
    3.         if (state)
    4.         {
    5.             gameOver = true;
    6.             if (double.Parse(scoreText.text) > GameState.current.highScore)
    7.             {
    8.                 GameState.current.highScore = double.Parse(scoreText.text);
    9.                 GlobalControl.Instance.highScore = double.Parse(scoreText.text);
    10.             }
    11.  
    12.             GlobalControl.Instance.lastScore = double.Parse(scoreText.text);
    13.  
    14.             SaveLoad.Save();
    15.  
    16.             StartCoroutine(LoadHomeScene());
    17.         }
    18.     }
    19.  
    20.     IEnumerator LoadHomeScene()
    21.     {
    22.         // The Application loads the Scene in the background at the same time as the current Scene.
    23.         //This is particularly good for creating loading screens. You could also load the Scene by build //number.
    24.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("HomeScene");
    25.  
    26.         //Wait until the last operation fully loads to return anything
    27.         while (!asyncLoad.isDone)
    28.         {
    29.             yield return null;
    30.         }
    31.     }
    However, once I get back to the home scene the gameObject title no longer moves like it did when the application first starts. I think it's noteworthy to point out that the play button still works. I've made sure all variables are reset via Start() and I've even tried to destroy the gameOjbect on disable so it'd be recreated, but still the issue remains. I'm working with scene changes for the first time here so I'm wondering if perhaps there's a second home scene being created or if the first one is being reused? Either way, the FixedUpdate() is not being called on the gameObject once the home scene starts for the second time. I placed a Debug.Log("got here") in Start() of the gameObject and it executes fine - however, when I place a Debug.Log("got here") in the FixedUpdate() it doesn't execute on the scene when re-loaded/re-created. Any insight on why the title gameObject would execute the FixedUpdate() initially but not when reloaded?? Thanks so much in advance! This problem has caused me hours of grief!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You got the log in Start() when you changed scenes, so the script is active.
    Unless you have some error in the console, I can't figure out from your code what could be wrong.
     
  3. goldenmindcreations

    goldenmindcreations

    Joined:
    Jun 10, 2017
    Posts:
    4
    Thanks for the reply. There are no errors in the console. I'm stumped as well! I'm thinking it has something to do with calling the scene for the second time. Perhaps a copy of the scene is persisting in the background and causing conflict? I can't find any information online on how calling a scene for the second time works.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Does something during gameplay or the game over screen set the timescale to 0?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Good point. Could be timescale.

    As for the scene persisting, that's not the case, from your code. It is gone when you change scenes and is loaded back, new, with your call to change scenes back.
     
  6. goldenmindcreations

    goldenmindcreations

    Joined:
    Jun 10, 2017
    Posts:
    4
    *face-palm* ...that's got to be it. I did temporarily set the timescale to 0 to hault the game before implementing the scene change method instead. You're awesome! Thank you!!!
     
  7. goldenmindcreations

    goldenmindcreations

    Joined:
    Jun 10, 2017
    Posts:
    4
    It was the timescale thanks for taking the time to help! I truly appreciate it.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) It's all good. Glad you got it solved.