Playerprefs value
Hello i have one problem with my code. im Making game with levels 1,2,3 etc. When i finished 1 level i set playerprefs on 2 and delete the image lock from 2 level. When i finish the level 2 set playerprefs on 3 and level 3 is available. 3 levels are available and i when to play level 1 again and finish playerprefs set again on 2 and lock my 3 level. itts any way to save the higest value from playerprefs? Sory for my bad english
Here is code:
void Start () {
if(PlayerPrefs.GetInt("NewLevel2Save") == 2)
{
GameObject.Find("lock2").GetComponent<Image>().enabled = false;
}
if(PlayerPrefs.GetInt("NewLevel2Save") == 3)
{
GameObject.Find("lock2").GetComponent<Image>().enabled = false;
GameObject.Find("lock3").GetComponent<Image>().enabled = false;
PlayerPrefs.Save ();
}
}
Thanks for help :)
Answer by MrReynevan2 · Jul 31, 2016 at 11:56 AM
@raqbiel On the script where you save the level number add an additional if:
if(PlayerPrefs.GetInt("Level2Save") < currentlyFinished) // check if the current lock is lesser than what you're playing right now
//If so, do the save.
That should prevent re-locking higher levels.
The only thing is, PlayerPrefs aren't the safest way to store game progression data, unless you add some kind of checksum.
Edit:
You may also want to change your Start()
method of the script above to something like this:
void Start () {
var _lock = PlayerPrefs.GetInt("NewLevel2Save");
for(int i = _lock; i > 1; i--)
GameObject.Find("lock"+i).GetComponent<Image>().enabled = false;
}
Or better yet, store a refence to these Images somewhere and just access them.
@$$anonymous$$rReynevan2 you are the best!! thanks for your answer:) now working nice :D