- Home /
Player Prefs for Current Lives
Could someone please explain to me how I can keep the current lives onto all my other scenes within the game I'm creating. I think I need player prefs but not sure how to implement this..?
using UnityEngine;
using System.Collections;
public class Lives : MonoBehaviour
{
// Need this for singleton
public static Lives Instance { get; private set; }
public int startingLives = 3;
public static int curLives;
// Need this for singleton
void Awake ()
{
if (Instance == null) {
Instance = this;
} else {
Destroy (gameObject);
}
}
// Use this for initializatio n
void Start ()
{
curLives = startingLives;
}
// Update is called once per frame
void Update ()
{
guiText.text = "Lives:" + curLives.ToString ();
}
// New method to decrementLives
public void decrementLives ()
{
curLives--;
}
// Increment Lives
public void incrementLives ()
{
curLives++;
}
}
Use DontDestroyOnLoad with your current object, so Lives script will always have the remaining lives.
For playerprefs;
PlayerPrefs.SetInt("Lives",curLives); to save
curLives = PlayerPrefs.GetInt("Lives"); to retrieve
PlayerPrefs.Has$$anonymous$$ey("Lives") to check if there is any save.
Thanks for the reply, I've replaced Destroy with DontDestroyOnLoad but where do you suggest I add the other script. I don't think it goes into the update as this updates every frame? (I hope I'm right with that comment.)
I do not understand, what other script?
you can put DontDestroyOnLoad to start or awake, it will not get destroyed if you load another scene, doesn't have to be in the update.
if you use DontDestroyOnLoad, you would have a single gameobject, which will track of your lives for the entire game, therefore, you would need to delete any instances of this from your scenes, except one, menu or first level.
if you use playerprefs, you can have different lives objects for each scene.
I'm meant what method I'd place the player pref code into it. I've added DontDestroyOnLoad but on each level three lives are shown even though on the previous level I had 4?
Answer by Bunny83 · Apr 26, 2014 at 02:42 AM
You can simplify your script quite a bit since it doesn't need to be a MonoBehaviour at all ;)
public class Lives
{
private static int m_InitialLives = 3;
private static int m_CurLives;
// static constructor
static Lives()
{
m_CurLives = PlayerPrefs.GetInt("lives", m_InitialLives);
}
public static int CurLives
{
get { return m_CurLives; }
set
{
m_CurLives = Mathf.Max(0, value); // clamp so it can't get negative
PlayerPrefs.SetInt("lives", m_CurLives);
}
}
}
With this class you can access your current lives from everywhere like this:
// read
if(Lives.CurLives > 0)
// decrement
Lives.CurLives--;
// increment
Lives.CurLives++;
Lives.CurLives+= 5;
// set
Lives.CurLives= 10;
It will be initialized with "m_InitialLives" the first time you access this script. Everytime you change the value it's stored in the PlayerPrefs so the next time you start your game it will load the last value.
If you prefer to attach the script to a gameobject you can simply add the playerpref code to your script:
//line 26 inside Start would look like:
curLives = PlayerPrefs.GetInt("lives", startingLives);
// After line 38 and 43 you would add this line:
PlayerPrefs.SetInt("lives", curLives);
Warning: You should only use decrementLives and incrementLives to change the value of curLives or the change won't be saved. That's why a property is way better for such things.
Thanks for that I appreciate the help with this. I managed to get it working with the second set of lines! Will look into incorporating the new class excluding the mono behaviour now! Do you $$anonymous$$d just explaining how I'd be able to add the lines of code to add three lives when its level one because currently its showing whatever lives are saved previously when restarting the game?
Answer by Robert Carlsson · Apr 26, 2014 at 02:09 AM
Here is a save script!
using UnityEngine;
using System.Collections;
public class SaveScript : MonoBehaviour {
public static SaveScript instance;
void Awake(){
if(instance == null)
instance = this as SaveScript;
else
Destroy(this);
}
public void Save(int lifes){
PlayerPrefs.SetInt("Life", lifes);
}
public int Load(){
return PlayerPrefs.GetInt("Life");
}
}
Then you call this in your other code when you want to save / load. Example: (Load in Start() and Save before you load next map.
So in Start you do:
void Start(){
curLives = SaveScript.instance.Load();
}
And when you want to save you do:
SaveScript.instance.Save(curLives);
Hope that made sense and was of use!
$$anonymous$$eep in $$anonymous$$d that you can pass a default value as second parameter to all the Get methods which is returned when the pref. isn't found (which is usually only the first time).
( btw it's one "life" but many "lives" :) )