Search Unity

"Load can only be called from the main thread." Error on changing scenes.

Discussion in 'Scripting' started by Thewerebean, Jan 28, 2016.

  1. Thewerebean

    Thewerebean

    Joined:
    Jan 28, 2016
    Posts:
    21
    Hello guys,

    I encounter a problem right now, and can't manage to fix it.

    I have two scenes, the first one is the menu and the second the game itself. When I run the game on the "game scene" it works fine, when I lose I go to the "Menu scene" via a method newGame > SceneManager.LoadScene("x") and I can start and play again and again...

    BUT, if I start the game on unity on the "Menu scene" then I have this strange message :

    "Load can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."

    The "Menu scene" is blank scene with one gui button, onclick > changeLevel().
    I created a script newGame with the function changeLevel on a null object in the scene.

    Thank you for yout help, I realy don't understand that error message!

    TWB
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please post the code (using the "Insert Code" button, located in the editing toolbar between the movie filmstrip and the floppy disk icon).
     
  3. Thewerebean

    Thewerebean

    Joined:
    Jan 28, 2016
    Posts:
    21
    The code of the NewGame script is:
    (It is a component of a null object, when I press the button it is supposed to call the funcion changeLevel)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. public class newGame : MonoBehaviour {
    6.  
    7.     public void changeLevel ()
    8.     {
    9.         SceneManager.LoadScene(+1);
    10.     }
    11.  
    12. }
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    And you're calling changeLevel from the Action event of a (new UI style) button?

    If so, that's probably fine; the real problem is elsewhere. As the error message suggests, you need to look for code executing from the constructors or field initializers of any script on any object in the scene.
     
  5. Thewerebean

    Thewerebean

    Joined:
    Jan 28, 2016
    Posts:
    21
    Unfortunatly there are nothing more in that scene!

    The fact is: if this scene is loaded from the other one, it works... I realy don't understand.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Does the problem occur as soon as you hit the Play button in this scene? Or do you have to actually press the button that triggers changeLevel to see the problem?
     
  7. Thewerebean

    Thewerebean

    Joined:
    Jan 28, 2016
    Posts:
    21
    The problem occures if I press the button, but evn withou the button, if the changeLevel method is called the game crash.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK then, the problem isn't on this scene, it's on scene 1. That's where some script is doing something naughty in a constructor or static initializer. (And in fact if you double-click the error message, Unity may take you right to it.)