Is it possible to save a button in PlayerPrefs?
I have 5 levels in my game. I would like to have a “Restart Level 2” button appear on the first scene of my project after the player reaches Level 2(Scene 2) and remains stored in PlayerPrefs just like my Highscore is saved in PlayerPrefs. Is this possible?
Below is my Highscore script using PlayerPrefs to store my highscore. I’d like to convert this script or add a function that will turn on a Level 2 Button. Maybe someone could point me in the right direction. I’m not sure how to do this.
Thanx for any input!
var r : float = 1;
var g : float = 0;
var b : float = 0;
var a : float = 1;
static var Score : int = 0;
function Start(){
Score+=100;
guiText.text = "High Score: "+Score;
var color : Color = Color (r, g, b, a);
// Change the material to display green text.
guiText.material.color = color;
}
function AddPoints() {
Score+=100;
guiText.text = "High Score: "+Score;
}
function Awake() {
Score = 0;
}
function Reset(){
Score = 1200;
}
function ResetLevel3(){
Score = 2100;
}
function ResetLevel4(){
Score = 3200;
}
function ResetLevel5(){
Score = 4400;
}
var HighestScore : int = 0;
var oldScore : int;
function AddScore(){
HighestScore = Score;
for(i=0;i<10;i++){
if(PlayerPrefs.HasKey(i+"HighScore")){
if(PlayerPrefs.GetInt(i+"HighScore")<HighestScore){
// new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i+"HighScore");
PlayerPrefs.SetInt(i+"HighScore",HighestScore);
HighestScore = oldScore;
}
}else{
PlayerPrefs.SetInt(i+"HighScore",HighestScore);
HighestScore = 0;
}
}
}
var Score1 : int;
function UpdateHScore(){
Score1 = (PlayerPrefs.GetInt("0HighScore"));
guiText.text = "High Score: "+Score1;
}
Below is my button script. “Quit” Button that appears after a player dies.
// Draws 2 buttons, one with an image, and other with a text
// And print a message when they got clicked.
var btnTexture : Texture;
function OnGUI() {
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(Rect(530,285,150,50),"QUIT"))
Application.LoadLevel("sk9");
gameObject.Find("GUIHighScore").SendMessage("AddScore");
gameObject.Find("GUIHighScore").SendMessage("UpdateHScore");
Time.timeScale =1;
}
Again. I’m trying to save a(any) button in PlayerPrefs so that if the player returns to play the game a week later he or she can access the last level they played. Any suggestions would be appreciated. Thanx
ps Please just make a “comment” if you don’t have a specific answer. Thanx