I have this simple class :
using System;
using System.Collections.Generic;
[Serializable]
public class SaveGame
{
public List<SaveObject> saveObjects;
}
Now I’m using it another script in two places in one method at the bottom :
public void Save()
{
SaveGame saveGame = new SaveGame();
saveGame.saveObjects = new List<SaveObject>();
SaveObject saveObject = new SaveObject();
for (int i = 0; i < objectsToSave.Count; i++)
{
saveObject.gameObjectInstanceID = objectsToSave*.gameObject.GetInstanceID();*
var x = objectsToSave*.GetComponents();*
var stateQueryComponent = x.Where(component => component is IStateQuery).ToList();
List componentsState = new List();
foreach (var z in stateQueryComponent)
{
var w = z as IStateQuery;
componentsState.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
}
saveObject.position = objectsToSave*.position;*
saveObject.rotation = objectsToSave*.rotation;*
saveObject.scaling = objectsToSave*.localScale;*
saveObject.componentsState = componentsState;
saveGame.saveObjects.Add(saveObject);
string json = JsonUtility.ToJson(saveObject);
SaveSystem.Save(json);
}
}
I’m doing :
saveGame.saveObjects.Add(saveObject);
And using a breakpoint I see that saveObjects contains one item.
Then in the Load method in the same script I’m doing :
public void Load()
{
Dictionary<int, Transform> instanceIdToObject = objectsToSave
.ToDictionary(o => o.GetInstanceID(), o => o);
var saveString = SaveSystem.Load();
if (saveString != null)
{
SaveGame saveGame = JsonUtility.FromJson(saveString);
foreach (var saveObject in saveGame.saveObjects)
Now when using a breakpoint on the foreach loop saveObjects list is empty.
If I will make something complex and make the saveObjects List public static then the list will be with the item/s I added but making it public static is not the solution.
What else should I do to reference to the same saveObjects list with the added items ?