- Home /
How to add playerPrefs to this script?
Hi there
I know this is very newbie, but its really urgent that I get help with this please :)
I have a menu Scene that has 9 Gui buttons with attached Textures to them and when clicked they take you to the relevant level.However they are all unlocked which makes the game pointless!
How would I go about adding PlayerPrefs to allow a level unlocking system. So from start of game only level one is not Greyed Out. After completing Lvl 1 then Lvl 2 is un-greyed and clickable and so on.
Thanks in advance , I am really battling with PlayerPrefs.
Here's The JS script:
public var button1 : Texture2D;
public var Button2 : Texture2D;
public var Button3 : Texture2D;
public var Button4 : Texture2D;
public var Button5 : Texture2D;
public var Button6 : Texture2D;
public var Button7 : Texture2D;
public var Button8 : Texture2D;
public var Button9 : Texture2D;
public var Button10 : Texture2D;
var btnTexture : Texture;
// JavaScript
function OnGUI () {
// Make a background box
// Make the first button. If it is pressed, Application.Loadlevel (1)
GUI.Box (Rect (1,200,1300,480),"Trial Version V1.0 Copyrite of GenY Tech Limited ©");
if (GUI.Button (Rect (10,230,200,200), button1)){
Application.LoadLevel(3);
}
if (GUI.Button (Rect (225,230,200,200), Button2)){
Application.LoadLevel (4);
}
if (GUI.Button (Rect (440,230,200,200), Button3)){
Application.LoadLevel (5);
}
if (GUI.Button (Rect (655,230,200,200), Button4)){
Application.LoadLevel (8);
Handheld.Vibrate();
}
if (GUI.Button (Rect (870,230,200,200), Button5)){
Application.LoadLevel (9);
Handheld.Vibrate();
}
if (GUI.Button (Rect (1085,230,200,200), Button6)){
Application.LoadLevel (10);
Handheld.Vibrate();
}
if (GUI.Button (Rect (250,10,900,100), btnTexture)){
Application.LoadLevel(1);
}
if (GUI.Button (Rect (10,440,200,200), Button7)){
Application.LoadLevel(11);
}
if (GUI.Button (Rect (225,440,200,200), Button8)){
Application.LoadLevel (12);
}
if (GUI.Button (Rect (440,440,200,200), Button9)){
Application.LoadLevel (13);
}
if (GUI.Button (Rect (655,440,200,200), Button10)){
Application.LoadLevel (6);
Handheld.Vibrate();
}
}
What PlayerPref
things have you tried in your battle?
First off all you need to add something to the playerprefs: https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html this can be a float int or a string (SetFloat,SetInt,SetString) then check foreach button(Level) if the playerpref is set(has$$anonymous$$ey). if it has the correct key you can show the button(by checking it) else you can not show it or whatever you want to do with it. Hope this helps.
Tip: You can also create all those buttons with a for loop.
I am sorry to do this
But do you have a sample of how I could use this in my script?
Thanks soo much
Tremayne
Answer by thornekey · Mar 11, 2014 at 11:16 AM
you could set an int (lets call it Lvl_1) to 0 for false and 1 for true.. then whenever you go to the menu have an if statement to check if PlayerPrefs.GetInt..etc if its 1 make it available.. if not keep greyed
now to make it greyed out and not actually usable you can do something like this:
GUI.Label (new Rect (10,230, 200, 200), "Level 1!", "Button");
it jsut makes it look like an unclickable button :)
This looks like it would work but I am having problems integrating this to my script. I sorry for being a pain but how would I do this exactly?
Thanks Tremayne
ok so a long-winded way of doing it from what we have from your code atm (i think.. on phone.. no compiler..)
if (PlayerPrefs.GetInt("Level1Available") == 1) {
if (GUI.Button (Rect (10,230,200,200), button1)){
Application.LoadLevel(3);
}
}
else if (PlayerPrefs.GetInt("Level1Available") == 0){
GUI.Label (new Rect (10,230, 200, 200), button1, "Button");
}
somethign like that i presume, but again, i cant check it in a compiler and its really late aha
Your answer
Follow this Question
Related Questions
Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts 0 Answers
PlayerPrefs Help 1 Answer
How would I transfer these into PlayerPrefs? 0 Answers
Is there a way to clear all the PlayerPrefs with names that start with a specific string? 1 Answer
Games played script using PlayerPrefabs 0 Answers