PlayerPrefs not resetting after death
Hey guys I am building a simple 2d Game and have come a long way so far. I started saving my collected coins and lives with PlayerPrefs and it works fine.
I created a level manager in which I made the starting lives "3 lives".
Now since putting the PlayerPrefs code in, I finally carry my collected coins and lives from level to level, but it seems it never resets on death. Even when I restart Unity Engine itself it still starts me with lets say 36 coins collected and 5 lives total. When in reality it should start me with 0 coins and only 3 lives.
Here is my LevelEnd script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LevelEnd : MonoBehaviour {
public string levelToLoad;
private PlayerController thePlayer;
private CameraController theCamera;
private LevelManager theLevelManager;
public float waitToMove;
public float waitToLoad;
private bool movePlayer;
public Sprite doorOpen;
private SpriteRenderer theSpriteRenderer;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
theCamera = FindObjectOfType<CameraController> ();
theLevelManager = FindObjectOfType<LevelManager> ();
theSpriteRenderer = GetComponent<SpriteRenderer> ();
}
// Update is called once per frame
void Update () {
if (movePlayer) {
thePlayer.myRigidbody.velocity = new Vector3 (thePlayer.moveSpeed, thePlayer.myRigidbody.velocity.y, 0f);
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player") {
//SceneManager.LoadScene (levelToLoad);
theSpriteRenderer.sprite = doorOpen;
StartCoroutine ("LevelEndCo");
}
}
public IEnumerator LevelEndCo(){
thePlayer.canMove = false;
theCamera.followTarget = false;
//theLevelManager.levelMusic.Stop ();
StartCoroutine ("fadeOut");
theLevelManager.levelMusic.Stop ();
theLevelManager.levelEndingMusic.Play ();
thePlayer.myRigidbody.velocity = Vector3.zero;
PlayerPrefs.SetInt ("CoinCount", theLevelManager.coinCount);
PlayerPrefs.SetInt ("PlayerLives", theLevelManager.currentLives);
yield return new WaitForSeconds (waitToMove);
movePlayer = true;
yield return new WaitForSeconds (waitToLoad);
SceneManager.LoadScene (levelToLoad);
}
IEnumerator fadeOut(){
for (float f = 1f; f >= 0; f -= 0.1f) {
theLevelManager.levelMusic.volume = f;
yield return new WaitForSeconds (.1f);
}
}
}
And here is my level manager Start script: void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
healthCount = maxHealth;
objectsToReset = FindObjectsOfType<ResetOnRespawn> ();
if(PlayerPrefs.HasKey("CoinCount")){
coinCount = PlayerPrefs.GetInt ("CoinCount");
}
coinText.text = "Coins: " + coinCount;
if (PlayerPrefs.HasKey ("PlayerLives")) {
currentLives = PlayerPrefs.GetInt ("PlayerLives");
} else {
currentLives = startingLives;
}
livesText.text = "Lives x " + currentLives;
}
Answer by Paratrooper82 · Jan 21, 2018 at 10:25 PM
Never Mind, I think I fixed this by adding:
PlayerPrefs.DeleteAll();
Right after my gameover screen plays. It seems that now when I restart the game, or even within the editor it starts out with my 0 coins and 3 lives. Is that the most efficient way of saving progress between levels, while at the same time making sure it doesn't carry over when you die?