- Home /
Saveing then loading the last unlocked level with continue button
I'm trying to save the game everytime the player reaches a portal and then when the game is stopped and when player wants to play again he can press the continue button on the startmenu to start at the level he last unlocked.
here is the tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
here is one of the scripts on the portal that should save the game:
using UnityEngine;
using System.Collections;
public class Saving : MonoBehaviour {
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Player")){
GameControl save = GetComponent<GameControl>();
save.Save();
}
}
}
gives no errors.
here is the script for the continue button:
using UnityEngine;
using System.Collections;
public class ContinueCS : MonoBehaviour {
void OnMouseUp(){
GameControl.control.Load();
int levelcount = GameControl.control.levelcount;
if(levelcount == 0){
Application.LoadLevel("Level1");
}
else
{
Application.LoadLevel(levelcount);
}
}
}
no errors
and here is the script based on the tutorial:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameControl : MonoBehaviour {
public static GameControl control;
public float score;
public int levelcount;
void Awake () {
if(control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if(control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.score = score;
data.levelcount = levelcount;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
data.score = score;
data.levelcount = levelcount;
}
}
}
[Serializable]
class PlayerData
{
public float score;
public int levelcount;
}
It would be great if someone could tell me what i'm missing or doing wrong. Thanks in advance
Answer by CyberWolf · Sep 09, 2014 at 09:57 PM
Its pretty late, but just at a glance, shouldn't the:
data.score = score;
data.levelcount = levelcount;
be:
score = data.score;
levelcount = data.levelcount;
in the load function?
you are right, i changed it but it still doesn't work, any other idea's?
I am also not so sure about:
GameControl save = GetComponent<GameControl>();
save.Save();
Because if both of the scripts are not connected to the same Gameobject I'm not sure if GetComponent will return what you are looking for.
Have you tried just calling:
GameControl.control.Save();
?
Aside from that it is hard to tell without knowing the whole picture. (For example, is it 2D or 3D? Because in 2D you need to use "OnTriggerEnter2D" and so on.)
You can try adding debug messages to various parts of the code to see if it even executes. (for example you can check if the trigger works)
http://docs.unity3d.com/ScriptReference/Debug.Log.html
Also you should check if the file itself is actually created.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do i Disable and Enable buttons? 2 Answers
How to access variables from an object based on location? C# (Pic inside) 1 Answer
Character ShopSave 0 Answers