- Home /
Question by
penguin21 · Oct 31, 2018 at 03:51 PM ·
serializationsavesave data
Help with serialization
I want to create a save file with the statistics of levels and levels unlocked but it does not leave me by line 96 I will pass the script and the saved class: Script:
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class SaveGame : MonoBehaviour {
public string saveName = "file1.sav";
public static Level[] levelsList;
public Level[] tempList;
public lvlPrefs[] lvlConf;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
levelsList = FindObjectsOfType(typeof(Level)) as Level[];
tempList = levelsList;
}
}
public static class SaveLoad
{
public static List<SaveFile> savedGames = new List<SaveFile>();
public static List<LevelSave> savedLevels = new List<LevelSave>();
//it's static so we can call it from anywhere
public static void Save(string fileName, MapPre statManager)
{
//SaveLoad.savedGames.Add(SaveFile.current);
BinaryFormatter bf = new BinaryFormatter();
//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
FileStream file = File.Create(Application.persistentDataPath + "/" + fileName); //you can call it anything you want
SaveFile data = new SaveFile(statManager);
bf.Serialize(file, data);
file.Close();
Debug.Log("Saved");
}
public static int[] Load(string fileName)
{
if (File.Exists(Application.persistentDataPath + "/" + fileName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = new FileStream(Application.persistentDataPath + "/" + fileName, FileMode.Open);
SaveFile data = bf.Deserialize(file) as SaveFile;
file.Close();
MapPre.tempMarioPos.x = data.mapPositionX;
MapPre.tempMarioPos.y = data.mapPositionY;
MapPre.tempMarioPos.z = data.mapPositionZ;
//List<Level> levels = (List<Level>)bf.Deserialize(file);
return data.stats;
}
else
{
Debug.LogError("File does not exist sorry :(.");
return new int[4];
}
}
}
[Serializable]
public class SaveFile
{
public static SaveFile current;
public int[] stats;
public float mapPositionX, mapPositionY, mapPositionZ;
public Level[] levelForSave;
public LevelSave[] levelData;
public lvlPrefs[] lvlC;
//public List<Level> levelList = new List<Level>();
public SaveFile(MapPre player){
stats = new int[5];
stats[0] = player.coinCurrent;
stats[1] = player.scoreCurrent;
stats[2] = player.currentItemBox;
stats[3] = player.currentLives;
stats[4] = player.currentPowerUp;
mapPositionX = player.marioPos.x;
mapPositionY = player.marioPos.y;
mapPositionZ = player.marioPos.z;
levelForSave = new Level[SaveGame.levelsList.Length];
lvlC = new lvlPrefs[SaveGame.levelsList.Length];
for (int i = 0; i < SaveGame.levelsList.Length; i++)
{
levelForSave[i] = SaveGame.levelsList[i];
Debug.Log(levelForSave[i].gameObject.name);
lvlC[i].lastCamName = SaveGame.levelsList[i].lastCameraName;
//levelData[i].lastCamName = levelForSave[i].lastCameraName;
}
//for(i)
}
}
Class:
[Serializable]
public class lvlPrefs
{
public GameObject obj;
public string sceneName, lastCheckpointName, lastCamName;
public bool StarCoin1, StarCoin2, StarCoin3, levelComplete;
}
Error code:
NullReferenceException: Object reference not set to an instance of an object
SaveFile..ctor (.MapPre player) (at Assets/Scripts/SaveGame.cs:96)
SaveLoad.Save (System.String fileName, .MapPre statManager) (at Assets/Scripts/SaveGame.cs:39)
MapPre.saveData () (at Assets/Scripts/MapWorld/MapPre.cs:53)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
help me pls
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612181311im_/https://answers.unity.com/themes/thub/images/avi.jpg)