Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 14, 2017 at 05:35 PM by Psinamen for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by Psinamen · Sep 10, 2017 at 05:47 PM · instantiatesavesavingsave data

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Sep 10, 2017 at 05:59 PM 1
Share

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.

avatar image Psinamen Cherno · Sep 10, 2017 at 06:15 PM 0
Share

Thanks! I'll check it out!

avatar image Psinamen Cherno · Sep 10, 2017 at 07:37 PM 0
Share

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.

avatar image ziadgapp · Sep 11, 2019 at 11:56 PM 0
Share

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.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by tormentoarmagedoom · Sep 10, 2017 at 06:20 PM

Yea

@Pogoda answer is good. Do that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
12

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Psinamen · Sep 10, 2017 at 07:27 PM 0
Share

Wow Thanks for the detailed explanation! It works wonders!

avatar image Pogoda-Kotwica Psinamen · Sep 10, 2017 at 07:57 PM 0
Share

$$anonymous$$ark the question answered then. You're welcome.

avatar image duyquang · Jan 08, 2019 at 09:03 AM 0
Share

Sry but can i have question for class GameData constructor. How you can save List objects with binaryformater.

avatar image Pogoda-Kotwica duyquang · Jan 08, 2019 at 04:12 PM 0
Share

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.

avatar image TomasPetrlik · Jan 13, 2019 at 07:14 PM 1
Share

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 ??

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving Player Data 3 Answers

How do I make a basic easy Save and load function with JavaScript 1 Answer

Problem with saving and loading game timer 0 Answers

Unity problem when i convert playerprefs save to the standard file io save 0 Answers

Save scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges