The question is answered, right answer was accepted
Saving current progression question
Ok, so I have two string arrays that I want to save and load on game start. All current daily quests are saved into one array (itemsList[]). All completed daily quests are saved in the other array (completedItemsList[]).
I have a Serialzation/Deserialization script for saving and loading the data.
At runtime, the current daily quests (itemsList[]) are loaded just fine. My problem is saving and loading the completed daily quests (completedItemsList[]).
When I click on a game object corresponding to a current daily quest, I want the name of that quest to be added to the completedItemList[] and then save the state. The completed daily quests do not save though.
This is my save/load Serialization/Deserialization code that saved the data to a file in binary.
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer(Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/1.yummy";
FileStream stream = new FileStream(path, FileMode.Create);
playData data = new playData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static playData LoadPlayer()
{
string path = Application.persistentDataPath + "/1.yummy";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
playData data = formatter.Deserialize(stream) as playData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save not found in " + path);
return null;
}
}
}
This is my data that I want to save and load:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[System.Serializable]
public class playData
{
public string[] allItems = new string[Player.itemsList.Count];
public string[] completedItems = new string[Player.completedItemsList.Count];
public int containCounter;
public playData (Player player)
{
int count = 0;
foreach(string a in Player.itemsList)
{
allItems[count] = a;
count++;
}
int cc = 0;
foreach(string c in Player.completedItemsList)
{
if(completedItems[count] != null)
completedItems[count] = c;
cc++;
}
}
}
This is where the actual data is saved/loaded:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public static List<string> itemsList = new List<string>();
public static List<string> completedItemsList = new List<string>();
public static int containCountHolder;
public void Start()
{
playData data = SaveSystem.LoadPlayer();
for (int count = 0; count < 100; count++)
{
itemsList.Add(data.allItems[count]);
completedItemsList.Add(data.completedItems[count]);
}
}
public void Update()
{
SaveSystem.SavePlayer(this);
}
}
I have an OnClick event for each game object corresponding to each current daily quest string. This onClick event should take the EventSystem.current.currentSelectedGameObject.name, and save that in the completedItemsList[].
When I start game trying to load compeletedItemsList[],, I get an error message like this:
When I load the game WITHOUT the completedItemsList, everything works pretty well.
Follow this Question
Related Questions
Saveing and Loading Problem 0 Answers
How do i Make this saving system apply the save data in my game. 0 Answers
[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer
how do i keep object destroyed after reloding scene ?? 0 Answers
Save location of created UI elements to JSON so it can load later 0 Answers