Need help with PlayerPrefs!

Here’s the code:

using UnityEngine;
using System.Collections;
using System;

public class Finish : MonoBehaviour {

	private Player bike;
	private CountDownTimer countDownTimer;
	public bool finished = false;
	private int episode;
	private int currentLevel;
	public int stars;
	private int getUnlock;
	public int starsLevel;

	private LevelButtons level;

	void Awake() {

		stars = 0;

		starsLevel = PlayerPrefs.GetInt("Episode:" +episode +"Level:"+currentLevel +"Stars:");

		print("STARS COLLECTED ALREADY: " + starsLevel);

		bike = GameObject.Find("PlayerBike").GetComponent<Player>();
		countDownTimer = GameObject.Find("GameScripts").GetComponent<CountDownTimer>();

		if (GameControl.control.episode1 == true)
		{
			episode = 1;
		}

		currentLevel = LevelButtons.levelControl.highestLevel + 1;

		// Check if level was finished before, and don't unlock new level if it was.
		getUnlock = PlayerPrefs.GetInt("Episode:" +episode +"Level:"+currentLevel +"Finished:");
		print(getUnlock);
	}
	

 	IEnumerator OnTriggerExit2D(Collider2D finish) {
		if (finish.gameObject.tag == "Player"  finished == false)
		{
			finished = true;
			if (finished == true){
				countDownTimer.StartTheTimer = false;
				bike.playerDisable = true;

				if (stars > starsLevel){
					PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel +"Stars:",stars);
					print("STARS COLLECTED: " + stars);
				}

				// Unlock new level if the level is finished for the first time.
				if (bike.StarsCollected > 0  getUnlock == 0){
					// Unlock en ny level.
					//PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel,1);
					// Legg antall stjerner i en PlayerPref.
					PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel +"Stars:",stars);
					
					PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel +"Finished:",1);
					
					print("LEVEL UNLOCKED!!");
				}

				yield return new WaitForSeconds(1);

				Application.LoadLevel("epLevels1");

			}
		}

	}

	void Update () {
		stars = bike.StarsCollected;
	}

My problem is this:

PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel +"Stars:",stars); won’t save!

So everytime I read the starsLevel = PlayerPrefs.GetInt("Episode:" +episode +"Level:"+currentLevel +"Stars:");
it is always equal to 0.

why?

:face_with_spiral_eyes::?

try PlayerPrefs.Save(); after you set the value.

For some reason it doesn’t work :S
If I set the PlayerPref.SetInt in the Awake function it works, but I need it to happen when the collision event occurs

Out of curiosity why do you have the IEnumerator on the line

IEnumerator OnTriggerExit2D(Collider2D finish)

shouldn’t it be
void OnTriggerExit2D(Collider2D other)
and using Trigger collision in the level?

I take it the print statements in this trigger method are telling you stars != 0?

Maybe try saving as Munchy said but on finishing the level at a single point, on this object disabling

No, since coroutines must return IEnumerator.

–Eric

But why are you using it as a coroutine when it is a MonoBehaviour message?

https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerExit2D.html

Why wouldn’t you use it as a coroutine? It’s perfectly reasonable. The docs specifically mention it (at least the docs for OnTriggerExit, but the same applies to OnTriggerExit2D).

–Eric

So they do must have missed that … glad I asked always good to find another way of doing something

Did you solve your original problem?

No I did not solve the problem.
Everything works just fine until I do this

PlayerPrefs.SetInt("Episode:" +episode +"Level:"+currentLevel,1);

Then all the info just disappears… I will try to do something other than PlayerPrefs, since they’re also unsafe