- Home /
how can i make one system that unlock levels by points?
In my game I have 10 levels. Each level has coins I'd like to use to unlock the next levels.
For example:
level 2: 10 coins
level 3: 20 coins
and so on ... how could I do that? Thanks in advance.
What have you tried so far? Do you need help keeping track of the number of coins collected between scenes? PlayerPrefs is a good way to do this.
I watch a video in youtube that taught about playerPrefs to save the colors of a cube.
He uses the following script:
pragma strict
var SavedColor;
function Start ()
{ SavedColor = PlayerPrefs.GetString("Color");
if(SavedColor != null)
{
if(SavedColor == "Yellow")
{
renderer.material.color = Color.yellow;
}
else
{
renderer.material.color = Color.red;
}
}
}
function Update ()
{
}
function OnGUI()
{
if(GUI.Button(Rect(100, 100, 100, 100),"Yellow"))
{
renderer.material.color = Color.yellow;
Save("Yellow");
}
if(GUI.Button(Rect(100, 200, 100, 100),"Red"))
{
renderer.material.color = Color.red;
Save("Red");
}
}
I think that with a few changes I can use this same script, just don't know where to begin
PlayerPrefs are just values are values that are held outside of scripts u can save Floats(decimal number values) ints(rounded number values) and strings(custom text), u do this like so.
//do this somewhere when u want to save
PlayerPrefs.SetString("Save/LoadName", "custom text to save");
// do this when u want to load it StartFunction is good for this as it only fires once.
string savedtext = PlayerPrefs.GetString("Save/LoadName");
Debug.Log(savedtext); // show what we loaded in the console.
PlayerPrefs.SetFloat("PositionX", transform.position.x);
float savedXPosition = PlayerPrefs.GetString("PositionX");
PlayerPrefs.SetFloat("ExperiencePoints", currentXP);
currentXP = PlayerPrefs.GetString("ExperiencePoints");
Answer by skylem · Feb 23, 2015 at 05:25 AM
Quite a simple goal really but how you go about it would depend upon how you setup your Levels and how the player accesses them for example if you click the levels from a main menu i've written a small example that assumes your levels are accessed by GUI but you should be able to make simple changes to implement it for your desired use.
// points we're checking.
int points;
// gui to be placed in OnGUI to enabled/disable buttons.
// check if we have 10 points
if(points > 10) {
// if we do have 10 points display a gui button for level 2
if(GUI.Button(new Rect(100,175,110,25),"Level 2")) {
Application.LoadLevel("1");
}
}
// the same as above except the points we check is 20 and
// the position height of the gui is a different value so
// they do not overlap, u can also use GUILayout to spread
// these buttons out over an area but i left it out as not
// to over complicate.
if(points > 20) {
if(GUI.Button(new Rect(100,205,110,25),"Level 3")) {
Application.LoadLevel("2");
}
}
this seems to be a good idea, now I have a point to start, thank you very much :)
no problem, when i first started i also had little to know idea on how to begin a concept, just something that kicks in when you learn a bit. and the best way to learn is by doing have fun if u require further help you can email me anytime Skylem@live.com.au
I am very grateful for the attention. Have a nice day :D
Answer by FoneFreak · Jul 24, 2017 at 07:34 AM
Hi friend, can this be applied to characters, ie unlock a character with 100 points? if so please advise if you can