.
.
CC#
Created by . on 1/20/2023 in #help
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
6 replies