- Home /
Load And Saving player prefs
I Made This Code That it Saves The (int) of the Scene by typing it in .... then when i Click the button it Loads The Level With The Right Number this error happens to me (ArgumentException: GetInt Can Only Be Called From The Main Thread) what should i do ??? and i tried adding it in the project but it didnt work !?
using UnityEngine;
using System.Collections;
public class LoadMenu : MonoBehaviour {
public int Level;
public void start (){
PlayerPrefs.SetInt ("Level", Level);
PlayerPrefs.Save ();
}
public void LoadLevel(){
if (PlayerPrefs.GetInt ("Level") == 0) {
Application.LoadLevel (0);
}
if (PlayerPrefs.GetInt("Level") == 1) {
Application.LoadLevel(1);
}
if (PlayerPrefs.GetInt("Level") == 2) {
Application.LoadLevel(2);
}
if (PlayerPrefs.GetInt("Level") == 3) {
Application.LoadLevel(3);
}
if (PlayerPrefs.GetInt("Level") == 4) {
Application.LoadLevel(4);
}
if (PlayerPrefs.GetInt("Level") == 5) {
Application.LoadLevel(5);
}
}
}
you're more likely to get help (remember most people are volunteers using their own free time) if your question is clear and provides relevant information.
yours isn't particularly clear. which game object is this script attached to?
did you confirm that "Level" exists in the PlayerPrefs
?
your start
function isn't called automatically by unity - do you call it from somewhere? maybe you meant to call it Start
?
since it's maybe not called, the PlayerPrefs
won't be saved by your script. LoadLevel
function could be rewritten like this:
public void LoadLevel()
{
var level = PlayerPrefs.GetInt("Level", 0); // Use default of 0 if "Level" not found
if ((level >= 0) && (level < 6))
{
Application.LoadLevel(level);
}
else
{
Debug.Log("ERROR: Invalid level number");
}
}
Your answer
Follow this Question
Related Questions
PlayerPrefs file wrong stored location 0 Answers
Need help with using Player Prefs to save number of coins collected. 1 Answer
How to make my game settings save? 1 Answer
GetInt can only be called from the main thread on Android 0 Answers
Setting, saving and getting info from PlayerPrefs, i can't save and call a highscore 1 Answer