How do you run multiple Playerprefs scripts (JS)?

Hello everyone!

I just learned about using playerprefs and so I decided to try to implement a highscore system, and a selection feature, where before starting the game the player can choose between three different spaceships, once selected the game will load the main level with the desired spaceship.

I have managed to implement these three features; however, I am having a problem with my highscore script where the value always remains 0. I also noticed that whenever I disable the load the desired spaceship script in the main level, the highscore functions properly, and so it appears that these playerprefs scripts are somehow conflicting with each other. I suspect that the problem would have to do something with the function HighscoreUpdate in the highscore script.

Any help on how I could fix this problem would be highly appreciated and if any further information is required please let me know, also thanks in advance!


The Spaceship Selection Script in the main menu:

/ Menu buttons
var logo				: GameObject;
var playButton			: GUITexture;
var helpButton			: GUITexture;
var creditsButton		: GUITexture;

// Additionaly Options

var shipsSelectionOBJ	: GameObject;

// Ship Selection Menu

var backText			: GUIText;
var shipSelectionText	: GUIText;
var shipSlow			: GUITexture;
var shipMid				: GUITexture;
var shipFast			: GUITexture;

// Statesments

var backTextPressed		: boolean;
var leveltoLoad			: String;
var playBPressed		: boolean;
var helpBPressed		: boolean;
var creditsBPressed		: boolean;

var shipSlowSelect		: boolean;
var shipMidSelect		: boolean;
var shipFastSelect		: boolean;

// Saving Options

function Update () {

	ShipSelection ();

}

function ShipSelection () {

	for ( var touch : Touch in Input.touches ) {

if ( touch.phase == TouchPhase.Began && shipSlow.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 1
		
				shipMid.guiTexture.enabled			= false;
				shipFast.guiTexture.enabled			= false;
				
				shipSlowSelect = true;
				
				PlayerPrefs.SetInt ("ShipSlow" ,1); // Save the selection of the player through giving it a name and value
				
				print ("Ship Slow selected!");
				
						backText.guiText.enabled = false;
						
						backTextPressed = true;
				
					yield WaitForSeconds (3);
				
				Application.LoadLevel (leveltoLoad);
				
					
		}
		
				if ( touch.phase == TouchPhase.Began && shipMid.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 2
		
				shipSlow.guiTexture.enabled			= false;
				shipFast.guiTexture.enabled			= false;
				
				shipSlowSelect = true;
				
				PlayerPrefs.SetInt ("ShipMid" ,2); // Save the selection of the player through giving it a and and value
				
				print ("Ship Mid selected!");
				
						backText.guiText.enabled = false;
						
						backTextPressed = true;
				
					yield WaitForSeconds (3);
				
				Application.LoadLevel (leveltoLoad);
				
					
		}
		
				if ( touch.phase == TouchPhase.Began && shipFast.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 3
		
				shipSlow.guiTexture.enabled			= false;
				shipMid.guiTexture.enabled			= false;
				
				shipSlowSelect = true;
				
				PlayerPrefs.SetInt ("ShipFast" ,3); // Save the selection of the player through giving it a and and value
				
				print ("Ship Fast selected!");
				
						backText.guiText.enabled = false;
						
						backTextPressed = true;
				
					yield WaitForSeconds (3);
				
				Application.LoadLevel (leveltoLoad);
					
		}
	}
}

The Spaceship loading script in the main level:

var shipSlowSelected 	: int = 0; // in order to enable the slow spaceship I must declare it as a interger...
var shipMidSelected		: int = 0;
var shipFastSelected	: int = 0;

var shipSlow			: GameObject;
var shipMid				: GameObject;
var shipFast			: GameObject;
var backText			: GUIText;

// Depending on the ship selected one of the three will re-spawn...

shipSlowSelected = PlayerPrefs.GetInt ("ShipSlow"); // Gets the int from the main menu which happens to be ShipSlow
 													
shipMidSelected = PlayerPrefs.GetInt ("ShipMid");

shipFastSelected = PlayerPrefs.GetInt ("ShipFast");


function Update () {
	
	for ( var touch : Touch in Input.touches ) {
	
		if ( touch.phase == TouchPhase.Began && backText.HitTest (touch.position)) {
		
			Application.LoadLevel ("Menu");
			
			}
		}
		
	ShipSelector ();
	
		
	if (Input.GetKey ("up")); {
	
	PlayerPrefs.DeleteAll ();
	
	}
}

// Which ship did the player choose?

function ShipSelector () {
	
	
	if ( shipSlowSelected == 1) { // if the playerpref value happens to be 1, then...
	
		shipSlow.renderer.enabled = true; // enable the slow spaceship
		//shipSlow.
	
	}
	
	if ( shipMidSelected == 2) { // if the playerpref value happens to be 2, then...
	
		shipMid.renderer.enabled = true; // enable the mid spaceship
		Debug.Log ("shipmid2");
	
	}
	
	if ( shipFastSelected == 3) { // if the playerpref value happens to be 3, then...
	
		shipFast.renderer.enabled = true; // enable the slow spaceship
		Debug.Log ("shipfast3");
	}
}

Highscore & Timer script in the main level:

// Timer

var time					: float;
var timer 					: int;
var timertext 				: String;
var timeActive  			: boolean;

// HighScore

var HighscoreText			: GUIText;
var playername 				: String; // optional feature, still work in progress

function Awake() {

HighscoreText.text = "HighScore: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed

}

function Update () {

	if (timeActive) {
	
		timer   = Time.timeSinceLevelLoad;
		time   += Time.deltaTime;
	}
	
	 HighscoreUpdate ();
}


function OnGUI () { // though ongui shouldn't be used for mobile, a simple task like this one is fine

//The gui-Time is the difference between the actual time and the start time.
var minutes : int = time / 60; //Divide the guiTime by sixty to get the minutes.

var seconds : int = time % 60;//Use the euclidean division for the seconds.

var fraction : int = (time * 100) % 100;
 
	timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
 
	GetComponent(GUIText).text = timertext;
 
}


function StopTimer2 () {
	
	timer = 0;
	timeActive = false;

}

function HighscoreUpdate () {

	// If the value of timer happens to be more than what the current scroe is...

     if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {

	// then update the new score, timerScore is a playerpref created which allows the player to save his highscore

			PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value

		}
}

You need to perform a save in order for your changes to a variable to take effect. It is done using

PlayerPrefs.Save();

So add this line after your line

PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value

from the last script above.

You also need to do the same where you set your ship variables.