- Home /
Saving a button in PlayerPrefs
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
no. I mean don't send me a link to PlayerPrefs reference page.
Answer by Mox.du · Nov 12, 2011 at 04:11 AM
Hi,
you could save Level Name (using Application.loadedLevelName) as string, or Level Number (using Application.loadedLevel) as int to your PlayerPrefs using SetString for string name or SetInt for int number. Like This:
PlayerPrefs.SetString("Level Name",Application.loadedLevelName); // for level name as string
PlayerPrefs.SetInt("Level Name",Application.loadedLevel); // for level number
PlayerPrefs.
Since you are saving current loaded level when player dyes, it will save the name or level number of the current level loaded.
Then you can display a button which will load level name into the button's text from PlayerPrefs, and on click will load appropriate level, like this:
if (GUI.Button(Rect(10,70,50,30),"Load "+ PlayerPrefs.GetString("Level Name")))
Application.LoadLevel(PlayerPrefs.GetString("Level Name"));
or
if (GUI.Button(Rect(10,70,50,30),"Load Level"+ PlayerPrefs.GetInt("Level Name")))
Application.LoadLevel(PlayerPrefs.GetInt("Level Name"));
Regards
Answer by syclamoth · Nov 12, 2011 at 04:07 AM
Well, you can store booleans in your PlayerPrefs as well- why don't you use a set of boolean values which tell the menu whether to activate the buttons? Alternatively, you could use an int for 'highest level reached' and set up the button using that instead.
so, having retrieved your button value-
if(displayLevel2Button)
{
if(GUI.Button(whatever))
{
Application.LoadLevel("LEVEL");
}
}
When you say set up the button with an int. Can you give me an example. thanx
When I say set up the button with an int, I mean keep a counter of what level you are up to, store that in PlayerPrefs as I assume you already know how to do, and then draw a button like this-
if(GUI.Button(Some Rect, "Load Level: " + levelNumber))
{
Application.LoadLevel(levelNumber + offset);
}
Where offset is the number of 'splashscreen' / menu scenes before the actual game levels in the level list.
Can you tell me what parts you don't understand? I'll try to explain it best I can.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
PlayerPrefs Question 0 Answers
DeleteAll not working on android? 0 Answers
how to save a load a game? 1 Answer
PlayerPrefs script problem 2 Answers