- Home /
Having trouble with PlayerPrefs in my script.
For some reason the variables SeedEntered and WorldCreated return back to 0 when I reload the level. But they should be staying at 1 because of the playerprefs. Does anybody know whats going wrong?
void OnGUI() {
if(PlayerPrefs.GetInt("Seed Entered") == 0)
{
if(GUI.Button(new Rect(10,400,1200,100), "Create the World"))
{
SeedEntered = 1;
PlayerPrefs.SetInt("Seed Entered", 1);
}
}
}
void Start()
{
//PlayerPrefs.DeleteAll(); //Used for testing new keys, comment out this code when not in use.
if(PlayerPrefs.HasKey("Player Seed"))
{
Seed = PlayerPrefs.GetInt("Player Seed");
}
else
{
Seed = Random.Range(0,10000);
PlayerPrefs.SetInt("Player Seed", Seed); //Sets Player Seed equal to Seed
}
}
void Update () {
if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
{
Random.seed = Seed;
}
else if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 0)
{
for(int z = 0; z < GameHeight; z++)
{
for(int x = 0 ; x < GameWidth; x++)
{
float rnd = Random.value;
if(rnd <0.25f)
{
Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
}
else if(rnd <0.5f)
{
Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
}
else
{
Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
}
WorldCreated = 1;
PlayerPrefs.SetInt("World Created", 1);
}
}
}
}
}
UPDATE: I did
Debug.Log(PlayerPrefs.GetInt("Player Seed"));
Debug.Log (PlayerPrefs.GetInt("Seed Entered"));
Debug.Log (PlayerPrefs.GetInt("World Created"));
And it says that seed entered and world created are still at 1. And the seed is carrying over too.
So how come it won't run this line of code when I return to the level?
if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
{
Random.seed = Seed;
}
Answer by robusto · Jun 09, 2014 at 06:33 AM
You need to Save your changes.
PlayerPrefs.Save();
http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html
So is your problem that you aren't generating tiles when you reload the level because "World Created" is set to 1, so that code that generates your tiles never gets executed because i t is saved to 1??? Because if you are reloading your level and your tiles disappear you shouldn't save your "World Created" to 1 in your PlayerPrefs.
but because of
if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
{
Random.seed = Seed;
}
shouldn't it just make the seed for the level the seed that I saved earlier?
So I'm guessing the PlayerPrefs.Save() now saves your values and it carries over. But it sounds as though that didn't fix your problem or it isn't the desired effect you were looking for. Can you describe in full detail what the new problem is?
When I return to the level it is gone. But the values are still saved at 1 and 1, so it should run
`if(PlayerPrefs.Has$$anonymous$$ey("Player Seed"))
{
Seed = PlayerPrefs.GetInt("Player Seed");
if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
{
Random.seed = Seed;
}`
However it clearly isnt.
However it clearly isnt.