- Home /
PlayerPrefs Loading Scene wont work
I am trying to build a script that would save the last scene you were on but it looks like it doesn't work by any means.By saving the last scene i mean saving the progress and sending you to Map Scene which is like a mainmenu So i have a principal Map scene where you have to press on one specific collider to send you to a Map1,2,3 etc. So the game logic would look like this if press on collider for the first time - sends you to map1 In map1 if you kill all the zombies - sends you to Map If press on collider again- sends you to map2 In map2 if you kill all the zombies - sends you to Map And so on.. I attached a script to my Map Scene which is not working
using UnityEngine;
using System.Collections;
public class DWP : MonoBehaviour {
public static int zombiesdeaths;
//private bool data = true;
//private bool stergeKey = false;
void Awake()
{
if(!PlayerPrefs.HasKey ("CurentMap"))
{
PlayerPrefs.SetInt ("CurentMap", 1);
}
zombiesdeaths = 9;
Debug.Log ("Awake");
//if(stergeKey == true)
// PlayerPrefs.DeleteAll ();
}
void OnMouseDown()
{
Debug.Log ("CLICK-UL MASII");
StartCoroutine ("Downwoods");
animation.Play ("testtouch");
}
IEnumerator Downwoods()
{
yield return new WaitForSeconds (2);
if(PlayerPrefs.GetInt("CurentMap")==1)
{
zombiesdeaths=9;
}
if(PlayerPrefs.GetInt("CurentMap")==2)
{
zombiesdeaths=9;
}
if(PlayerPrefs.GetInt("CurentMap")==3)
{
zombiesdeaths=9;
}
print ("ienumerator");
Application.LoadLevel("Map"+PlayerPrefs.GetInt("CurentMap").ToString());
}
}
And the Map1,Map2,Map3 Script function is the following:
void Kill ()
{
DWP.zombiesdeaths--;
Debug.Log (DWP.zombiesdeaths );
if(DWP.zombiesdeaths<1)
{
//Time.timeScale = 0;
Debug.Log("ADIO");
PlayerPrefs.SetInt("CurentMap",PlayerPrefs.GetInt("CurentMap")+1);
Application.LoadLevel("MapScene");
}
Destroy( gameObject );
}
Answer by MrSoad · Nov 30, 2014 at 11:10 AM
You are setting your "CurrentMap" player pref, but you never save it so it is not saved :
http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html
Your answer
Follow this Question
Related Questions
Changing Scenes using Unity5 AssetBundle 1 Answer
Is it safe to save values in a prefab instead of using PlayerPrefs? 0 Answers
PlayerPrefs after 0 lives, want to load another scene. 0 Answers
How to use PlayerPrefs to stop an item from reappearing when you return to the level 2 Answers
Save/Load Variable with playerprefs 1 Answer