Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Crash when using script after load scene

Discussion in 'Scripting' started by Topfall, May 16, 2024.

  1. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
    Hello,

    When I run my script after loading the scene with a TMP UI Button it crashes:
    Code (CSharp):
    1. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    or
    Code (CSharp):
    1. SceneManager.LoadScene("Test");
    When load the scene from another scene either with or without a button it does not crash:
    Code (CSharp):
    1. void Start()
    2. {
    3.    //SceneManager.LoadScene("Test");
    4. }
    5.  
    6. public void Test()
    7. {
    8.     SceneManager.LoadScene("Test");
    9. }
    Note- I used loadscenemode.single and it does not work; It does not crash from the scene loading itself, but rather when I use a script after loading the scene. I am pretty sure that the other script used after the load does not relate because it only triggers in this specific situation.

    I've also tested it as a build and it crashes then too. Can anyone help me fix this issue? Thanks to any help.
     
    Last edited: May 16, 2024
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
  3. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,417
    Then what does the editor log say?
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,417
    Upload the file. Don't just dump the text here, especially as a forum post can't have that many characters in it.
     
  6. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Add a Debug.Log wherever you're loading a scene to see how many times your loading the scene, and to verify it's the right scene.
     
  7. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
    Here-
     

    Attached Files:

  8. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
    I only load it once, and it doesn't crash until I interact with any scripts
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,417
    To be fair, looking at your code here:
    Code (CSharp):
    1. void Start()
    2. {
    3.    //SceneManager.LoadScene("Test");
    4. }
    Were you loading the same scene with this? This will just go infinite and indeed crash the editor.
     
  10. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
    Yeah I used this (not commented) in another script and another scene, it did not crash like it does from the same scene with:

    Code (CSharp):
    1. public void ResetScene()
    2. {
    3.     Debug.Log("1");
    4.     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    5. }
    used on a TMPro UI button
     
  11. Topfall

    Topfall

    Joined:
    Dec 31, 2020
    Posts:
    15
    I fixed it by removing a while loop from another script. Not sure why it is connected but thanks for the help