So I have a basic checkpoint system up but I want to make it work as a real saved checkpoint, I have been looking into persistent data tutorials but they all seem to revolve around saving scores etc. rather than positions and also from what I have seen commands like dontdestroyonload only work for when the runtime is already active which is not what I’m looking for.
What I need is a bit of code or tutorial that can tell me how to keep this data even after I have exited the program, so for example, when I load up the game again and press the continue button the game will go back to the last checkpoint the player can go through, all the examples I’ve been so far don’t keep the data when you exit the game.
going to have to wrap my head round this, in the tutorial I’ve followed they did actually use this command and they used GetInt but how would I save it to a reg file that this documentation is going on abo-…
Nevermind found it while I was typing
It looks like I just didn’t do as much digging on playerprefs as on the other stuff I’ve been looking at. So just to double check guys, what the playerpref command does is save everything in run time and then the save function actually writes it to the disk so it won’t be erased when it loads?
Won’t be erased when the application quits… yes. Correct.
Unity should automatically load the saved Player Prefs data and if you have saved some data you should be able to retrieve it. If you have not saved it yet then errors will occur so check Player Prefs has a key before trying to retrieve a key.
Well it all seems really self-explanatory but as usual I’m doing something wrong that I’ve completely missed, just glanced over this tutorial here but it’s still resetting itself and I think I might have a bit of code that’s causing the issue.
I thought it was the function I was told to write in a different tutorial I was followng that makes sure that you can’t get negative integers in the game but even when removing that code the game doesn’t seem to be saving my score.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public static int score;
Text text;
// Use this for initialization
void Start ()
{
text = GetComponent<Text> ();
score = PlayerPrefs.GetInt ("CurrentScore", score);
}
// Update is called once per frame
void Update ()
{
if (score < 0)
score = 0;
text.text = "" + score;
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
PlayerPrefs.GetInt ("CurrentScore",score);
}
public static void Reset()
{
score = 0;
PlayerPrefs.GetInt ("CurrentScore",score);
}
}
Surely such a simple function should just work right? The only thing I can think of is that some code somewhere is conflicting with playerpref.save();
Thank you! That fixed it! So it looks like if you make any changes it needs to be SetInt so that the game remembers the new integer, now I just need to look into how to clear all the data and set it back to normal and then we’re good to go.
You need to use SetInt not GetInt.
GetInt will get an int from your PlayerPrefs and SetInt will set the int in your player prefs.
Using Save will force a save of your PlayerPrefs to disk.
You don’t “need” to use Save as it saves automatically when the application quits but it is recommended to use Save incase your app crashes where appropriate.
An appropriate place to use save might be at the end of a level where it doesn’t matter if you get a slight hiccup in frame rate.
It looks like it’s actually very easy to use SetInt when it comes to basic numbers but I’m back to square one on getting everything to work so that the game records what checkpoint position the character is at.
This is the basic code I’m working with that I got going with the help of the tutorial and I just don’t have any idea how to get the player to instantiate to the current checkpoint as opposed to the first checkpoint.
LevelManager script
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
private PlayerController player;
public GameObject currentCheckpoint;
public GameObject DeathParticle;
public GameObject RespawnParticle;
public float respawnDelay;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
StartCoroutine ("RespawnPlayerCo");
}
public IEnumerator RespawnPlayerCo()
{
Instantiate (DeathParticle, player.transform.position, player.transform.rotation);
player.enabled = false;
player.GetComponent<Renderer>().enabled = false;
Debug.Log ("Player Respawn");
yield return new WaitForSeconds (respawnDelay);
player.transform.position = currentCheckpoint.transform.position;
player.enabled = true;
player.GetComponent<Renderer>().enabled = true;
Instantiate (RespawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
}
Checkpoint script
using UnityEngine;
using System.Collections;
public class Checkpoint : MonoBehaviour {
public LevelManager levelManager;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.name == "Player")
{
levelManager.currentCheckpoint = gameObject;
Debug.Log ("Activated Checkpoint" + transform.position);
}
}
}
The checkpoint script sets the current checkpoint for you so I’m wondering if I need to do something with the trigger using playerprefs. By the way, this one might be simpler, but does anybody know how to keep objects persistently destroyed, this is all for a platformer I’m doing and I want to make sure I can have a proper chekcpoint system going.
Okay, after doing quite a bit of research it looks like I need to take a close look at persistent data path and serialization if I want to store gameobjects and scripts etc. properly, it seems that playerprefs only really works well for simple numbers and text.
Looks like I’ve got something very new to learn if I want to get proper saving and loading going in my game, I think to be safe I will probably make a new project and just write up some simple checkpoint code to try and get everything working.
Official tutorial from Unity ( Yey! ):
The official tutorial goes into seralization about halfway through.