PlayerPrefs saving bools to unlock a level
Hi all..... i dont want to bother you with this frequently asked question about playePref.... but i cant understand whatever i am trying...... I have a trigger and when the trigger is entered my bool goes true...... but when i quit the application it restarts and the bool is false once again......
using UnityEngine; using System.Collections;
public class trigger : MonoBehaviour {
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "cube"){
Application.LoadLevel(0);
levelhi.locklvl = true;
}
}
}
//
and this is the menu level managers...
using UnityEngine;
using System.Collections;
public class levelhi : MonoBehaviour {
public static bool locklvl = false;
public void OnClick() {
if(locklvl==true)
Application.LoadLevel (2);
}
// Use this for initialization
void Start () {
if(locklvl==false){
Destroy(gameObject);
}
}
}
Answer by DroidifyDevs · Jan 31, 2016 at 12:19 AM
You aren't properly saving the PlayerPrefs. PlayerPrefs are tricky to learn, but once you get the hang of them they are SUPER EASY. Watch this tutorial, this is how I learned: https://www.youtube.com/watch?v=AzypHv5nBEc
And read the docs: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
PlayerPrefs are pretty much the same in C# as in JS.
Also, if you're having issues with bool, try using Int. While it isn't "correct", for many purposes an Int value will work just as well as a bool. For example whenever I need to make a switch, the "correct way" is to use Bool True/False, but I use Int = 1 for True and Int = 0 as false. Easier for me :)
Don't forget to accept the best answer :)
Your answer
Follow this Question
Related Questions
How Asus xtion2 is used in Unity 0 Answers
2019.3 game foundation save 0 Answers
Interupting timer causes the next attack to stop the script 0 Answers
Character Contoller Script 0 Answers