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. Dismiss Notice

Call a function after the scene is loaded.

Discussion in 'Scripting' started by heh223, Dec 4, 2021.

  1. heh223

    heh223

    Joined:
    Jan 2, 2020
    Posts:
    25
    I’m trying to call a function from a specific script after a specific scene is loaded. I don’t actually know how this is possible. I tried SceneManager.sceneLoaded but this is probably not the way to do it. Below is my code (probably a very bad code). First, I'm trying to load a specific scene then I want to call a function »Load« from a specific script that will load saved data. What happens is actually that all saved game is loaded and the scene after that, but I want the opposite.

    Code (CSharp):
    1.  
    2. public GameObject reloadScene;
    3.  
    4. public void OnEnable()
    5. {
    6.     SceneManager.LoadScene("Game");
    7.     SceneManager.sceneLoaded += OnSceneLoaded;
    8. }
    9.  
    10. void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    11. {
    12.     SimpleLoad loadStuff;
    13.     loadStuff = GameObject.FindGameObjectWithTag("SimpleLoad").GetComponent<SimpleLoad>();
    14.     loadStuff.Load();
    15. }
    16.  
    17. void OnDisable()
    18. {
    19.     SceneManager.sceneLoaded -= OnSceneLoaded;
    20. }
    21.  
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Is anything preventing you from loading the save in the "Game" scene instead?
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,405
    OnEnable gets called after sceneLoaded (for something in the scene to be enabled you need to have the scene loaded first)
    Just do it in Awake in your game scene, it should work a lot easier then without all the events
     
  4. heh223

    heh223

    Joined:
    Jan 2, 2020
    Posts:
    25
    It is really confusing for me. I tried with Awake but it doesn't work for me or I just don't do it the correct way.

    So I have a button on which I have set On Click () and set it to use a function OnEnable from the script above. The game actually reloads and I get a NullReferenceException on the 13th line of the script which I posted above. I tried everything that came to my mind. If I use Awake it doesn't really do anything because it calls only Awake function. Is there any idea how to solve this, so the game scene will load and after that function Load() will get called so data from the saved file gets used/loaded to the game.

    PS: this save/load system is really a pain in the ass. xD
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Your head is still stuck in the way you want to do it.
    We are both recommending the script that loads your save, be in the "Game" scene instead.
    But as I tried to ask, is anything stopping you from doing that?
     
  6. heh223

    heh223

    Joined:
    Jan 2, 2020
    Posts:
    25
    Now I see what you two had in mind and I made it work. Thank you very much! :D :D
     
    DevDunk likes this.