- Home /
2 errors making a loading scene
I need to do a loading scene that load different level from saves,
i made a empty scene only with the main camera and added to it :
loading.js:
#pragma strict
function Start () {
var livello : String;
if(PlayerPrefs.GetString("Livello") != null)
livello = PlayerPrefs.GetString("Livello");
else
Application.LoadLevel("main menu");
if(livello.Equals("livello 2"))
Application.LoadLevel("main menu 2");
}
With this code unity gives me 2 errors:
GetString 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.
ArgumentException: GetString 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.
UnityEngine.PlayerPrefs.GetString (System.String key) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/PlayerPrefsBindings.cs:73)
loading..ctor () (at Assets/Scripts/loading.js:2)
The error message suggests a fix:
move initialization code to the Awake or Start function.
Did you try that?
Answer by xDevily · Aug 02, 2014 at 09:19 AM
I solved my problem, i forgot to add an else to load main level, The two errors weren't in the script that i posted, they were a console "log" that i don't delete
Answer by BerggreenDK · Aug 01, 2014 at 10:56 PM
I believe the variable you call livello is local to start and not a "global" in your class/script..
var livello : String;
try moving that outside your function Start()
Not sure if this is the reason, but I think it is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155605im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Editing an XML File 1 Answer
Scene not loading properly everytime 3 Answers
How i can PlayerPrefs List<>? 0 Answers
My Load function isn't working and now I'm getting an error.. 0 Answers
iOS - WWW Doesn't work on Local Load 1 Answer