public DataObject SaveGame()
{
DataObject data = new DataObject();
persistentObjects.ForEach((k, v) =>
{
DataObject sub = new DataObject();
foreach (var o in v.Value)
{
sub.Set(o.name, o);
}
data.Set(v.Key, sub);
});
return data;
}
public void LoadGame(DataObject data)
{
if (data != null)
{
Dictionary<string, DataObject> objects = data.GetData<DataObject>(typeof(DataObject));
objects.ForEach((k, v) =>
{
Dictionary<string, GameObject> gObjects = v.Value.GetData<GameObject>(typeof(GameObject));
//gObjects is always null here
persistentObjects.Add(v.Key, gObjects.Values.ToArray());
});
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
The issue you're facing is the following: you can't store game objects (spawned or prefabs, or any other kind of asset like audio clips, textures, etc.) in save games.
The data serialization of settings can store references to assets, but not save games.
You'd need to have some custom serialization to do that, e.g. using the custom component save data to store the needed information of the game object to be able to recreate it on load game.
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
I mean the easy answer is just store something that identifies the prefab it comes from, position, rotation, scale, and recreate from that. At the same time this seems likely to become a giant rabbit hole, so to speak.
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
One other thing, would it be possible to set an object's name when spawning a prefab from an event? i want to be able to control the hierarchy name so that it is easier to find later on.
- component name: Transform
- add a field
- field name: name
- is property: enabled
- field type: String
- string value: whatever you want to name it
And don't forget to select the object (actor, prefab, etc) you want to change.
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!