C
C#2y ago
lyxerexyl

Help, how to make this unity save and load system work?

using UnityEngine;
using System.Collections.Generic;
using System.IO;

public class TestSaveSyst : MonoBehaviour
{
public List<GameObject> ListOfGameObjects;
public List<NormalObject> objectsToSave;
public List<NormalObject> LoadedObjects;
public string fileName = "savedObjects.json";

public ListObjectToSave listObjectToSave;

void Awake()
{
listObjectToSave = new ListObjectToSave();
}

void OnEnable()
{
objectsToSave = listObjectToSave.objectsToSave;
}

public void GOListToNOList() // Turns ListOfGameObjects to objectsToSave
{
foreach(GameObject go in ListOfGameObjects)
{
objectsToSave.Add(TurnGameObjectToNormalObject(go));
}
}

public NormalObject TurnGameObjectToNormalObject(GameObject go)
{
NormalObject newNorm = new NormalObject();
newNorm.name = go.name;
newNorm.x = go.transform.position.x;
newNorm.y = go.transform.position.y;
return newNorm;
}

public void Save()
{
string json = JsonUtility.ToJson(objectsToSave);
File.WriteAllText(Application.dataPath + "/" + fileName, json);
}

public void Load()
{
if (File.Exists(Application.dataPath + "/" + fileName))
{
string json = File.ReadAllText(Application.dataPath + "/" + fileName);
LoadedObjects = JsonUtility.FromJson<List<NormalObject>>(json);
}
else
{
Debug.Log("File not found!");
}
}
}

public class ListObjectToSave
{
public List<NormalObject> objectsToSave;
}

[System.Serializable]
public class NormalObject
{
public int id;
public string name;
public float x;
public float y;
}
using UnityEngine;
using System.Collections.Generic;
using System.IO;

public class TestSaveSyst : MonoBehaviour
{
public List<GameObject> ListOfGameObjects;
public List<NormalObject> objectsToSave;
public List<NormalObject> LoadedObjects;
public string fileName = "savedObjects.json";

public ListObjectToSave listObjectToSave;

void Awake()
{
listObjectToSave = new ListObjectToSave();
}

void OnEnable()
{
objectsToSave = listObjectToSave.objectsToSave;
}

public void GOListToNOList() // Turns ListOfGameObjects to objectsToSave
{
foreach(GameObject go in ListOfGameObjects)
{
objectsToSave.Add(TurnGameObjectToNormalObject(go));
}
}

public NormalObject TurnGameObjectToNormalObject(GameObject go)
{
NormalObject newNorm = new NormalObject();
newNorm.name = go.name;
newNorm.x = go.transform.position.x;
newNorm.y = go.transform.position.y;
return newNorm;
}

public void Save()
{
string json = JsonUtility.ToJson(objectsToSave);
File.WriteAllText(Application.dataPath + "/" + fileName, json);
}

public void Load()
{
if (File.Exists(Application.dataPath + "/" + fileName))
{
string json = File.ReadAllText(Application.dataPath + "/" + fileName);
LoadedObjects = JsonUtility.FromJson<List<NormalObject>>(json);
}
else
{
Debug.Log("File not found!");
}
}
}

public class ListObjectToSave
{
public List<NormalObject> objectsToSave;
}

[System.Serializable]
public class NormalObject
{
public int id;
public string name;
public float x;
public float y;
}
It is a unity script. I added a group of gameObjects in the ListOfGameObjects. Manually called GOListToNOList()(Worked) then called Save(). Save() worked also, but only gave me an empty json, just a pair of brackets. Thanks
5 Replies
Auros
Auros2y ago
According to the Unity documentation https://docs.unity3d.com/ScriptReference/JsonUtility.ToJson.html, it appears you are not allowed to serialize an array and presumably List<T> directly, as it tries to serialize the public fields on the List object itself, not the contents within. You need to wrap the list in a separate serializable class or struct, which you seem to already have with ListObjectToSave but aren't using. Try replacing this line in your Save method from
string json = JsonUtility.ToJson(objectsToSave);
string json = JsonUtility.ToJson(objectsToSave);
to
string json = JsonUtility.ToJson(listObjectToSave);
string json = JsonUtility.ToJson(listObjectToSave);
and change the type and assignment of the JsonUtility.FromJson call in your Load method. You might also need to add a [System.Serializable] attribute to the ListObjectToSave class.
lyxerexyl
lyxerexyl2y ago
Omg, I guess that worked. You guys are op. You figured that out just from the documentation even if you are not into unity.... I was staring at that for like 2 hours already. Alright, cheer thanks!!
Auros
Auros2y ago
I do Unity, but I've never touched the JsonUtility class. I used to use Newtonsoft but now I use System.Text.Json
lyxerexyl
lyxerexyl2y ago
oh, wierd you're not in unity discord. And Im not familiar with those 2 json library, but might be worth studying for the next time
Auros
Auros2y ago
Oh I never realized there was an official Unity discord server