- Home /
The question is answered, right answer was accepted
Saving a game object instantiated after runtime.
Greetings! I'm rather new to Unity/C#, and I have a bit of a problem when saving data. I have a game object that, when the player interacts with it, becomes a new and different game object.
I've been looking for a way to save that change. To make matters worse, I'm spawning the new object from a Prefab (Because it's sort of a building game), so I would need to generate an ID for the new object upon instantiation.
I've already watched the tutorial on persistence, and while that works fantastically to save data of gameobjects present at runtime, it's working for this.
Edit For clarification: Is there a way to save game objects created after runtime, so they can be loaded later?
Thanks!
Saving GameObject is fairly difficult and complex to do, unfortunately. I might want to look at my free Unity Save Load Utility that provides one way to do it.
I have a question about how to use your plugin, if it's not much to ask (I swear I read the manual and quick-start guide).
I've added the SaveLoad Utility and Persistence $$anonymous$$arker to an empty gameobject, and added the ObjectIdentifier to every object I want to save.
But after that, I'm not sure what I should be doing with OnLoad and OnSave. Should I include them on every script of an object I want to save? What should I put inside of them, the data I want to save? Thanks, and sorry if I'm asking too many questions.
Simple - just run your game, and before you end it, copy the gameobjects you want (Ctrl+C). Stop your game from running now (back in Editor mode), and paste them! (Ctrl+V). Helps a ton for level design in my opinion.
Answer by tormentoarmagedoom · Sep 10, 2017 at 06:20 PM
Yea
@Pogoda answer is good. Do that.
Answer by Pogoda-Kotwica · Sep 10, 2017 at 07:08 PM
Because you can save only simple types like floats, strings, ints, bools, and arrays and lists of those you need a system that will go from game objects and its components to data and back again. Let's say you need to store position only. You should start with creating a class that stores some kind of saveable prefab ID and whatever you want to save, position in this case.
[System.Serializable]
public class GameObjectSaveData
{
public int id;
public float x;
public float y;
public float z;
// put constructor here
}
It is System.Serializable so you can actually save it. Now let's create some simple container for those;
[System.Serializable]
public class GameData
{
public List < GameObjectSaveData "> objects;
// put constructor here
}
Now something for saving it. Two scripts, one calls an event to every saveable game object and second who actually makes a game object saveable.
public static class GameSaveLoad
{
public delegate void SaveData(GameData Data);
public static event SaveData SaveDataEvent;
public void Save() {
GameData data = new GameData();
if (SaveDataEvent != null)
SaveDataEvent(data);
BinaryFormatter bf = new BinaryFormatter();
...
Now u should know the rest of it after watching the tutorial. And the game object script
public class SavingComponent : MonoBehaviour {
public int id;
private void OnEnable()
{
GameSaveLoad.SaveDataEvent += Save;
}
private void OnDisable()
{
GameSaveLoad.SaveDataEvent -= Save;
}
public void Save(GameData data)
{
data.objects.Add(new ObjectData(id, transform.position.x, //etc etc ))
}
So now you have them saved. Loading is the rest. You said you know how to instantiate them - you only need some kind of Spawner with Spawn function, that will choose specific prefab from a list basing on its ID and put it in right place.
You really should make this kind of list with ScriptaleObject. If you are unfamiliar with events I can explain them. I haven't type the code into Unity so it might not work, but it surely shows the idea and my own Save System is like this.
My English is not perfect so sorry if I made anything unclear.
Wow Thanks for the detailed explanation! It works wonders!
$$anonymous$$ark the question answered then. You're welcome.
Sry but can i have question for class GameData constructor. How you can save List objects with binaryformater.
You don't save a list of objects (C# objects or UnityObjects). You can only save simple types and objects that are composed of them and marked as [Serializable]. What you want to save is object's ID - this might be an index of a given game object on a prefab list or something similar. Then you instantiate saved game object using the ID and recreate its state using saved data.
I don´t really understand. I am new to save/load systems and I understand that you can only save stuff like bools, int, float, etc. but what I need for my game is to save instantiated object in runtime, basicaly what @Psinamen asked, but what you mean by // put constructor here ??