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 /
avatar image
0
Question by PultroN93 · Apr 17, 2019 at 04:20 PM · gameobjectsaveloadjsonfile

Saving GameObject to file

Hello, I'm new in topics like saving game progress and I have a question. What I need is to save whole GameObject to file on button click (when game is closing) and later when turning game again on I want to Instantiate that GameObject from saved file. Is there any way to do that? What i tried now is:

Doing class with GameObject:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     [System.Serializable]
     public class SaveLevel
     {
         public GameObject Object;
     
         public SaveLevel(GameObject object)
         {
             Object = object;
         }
     
     }
     

And voids for buttons to save and load it:

         public void SaveLevelOnButton()
         {
             SaveLevel save = new SaveLevel(/*GameObject i want to save. Let's say:*/ ObjectToSave);
             string json = JsonUtility.ToJson(save);
             File.WriteAllText(Application.persistentDataPath + "/LevelState.json", json);
         }
         public void LoadLevelOnButton()
         {
             GameObject LoadedLevel;
             string json = File.ReadAllText(Application.persistentDataPath + "/LevelState.json");
             LoadedLevel = JsonUtility.FromJson<SaveLevel>(json).gameObject;
     
             Instantiate(LoadedLevel, new Vector3(0, 0, 0), Quaternion.identity);
         }

That code is creating the GameObject "ObjectToSave" from begin like it wasn't saved.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by xbassad · Apr 17, 2019 at 05:59 PM

If you want to save your game progress just use PlayerPrefs.Save()

Comment
Add comment · Show 3 · 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 PultroN93 · Apr 17, 2019 at 07:40 PM 0
Share

$$anonymous$$y GameObject to save has more GameObjects in it as childerns and every of this children has a lot of data. That's why I wanted just to save the GameObject. Do you think when i turn game off and on, will i get the data like they were on quiting the game? CanPlayerPrefs do that?

avatar image xbassad PultroN93 · Apr 17, 2019 at 08:58 PM 0
Share

Check the documentation https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

and also check this video

https://www.youtube.com/watch?v=BgxbCej0GOg

avatar image PultroN93 xbassad · Apr 17, 2019 at 09:15 PM 0
Share

So, PlayerPrefs is a good way to save the data. I know now that i can't save whole GameObject. I'm going to change my scripts so I can save the variables. Thanks for your time!

avatar image
1

Answer by NikitaDemidov · Apr 17, 2019 at 07:52 PM

I wouldn't save GameObject. I would save some Attributes. Example: A normal 3D cube: "P-x=" + obj.transform.position.x + "P-y" + obj.transform.position.x ... You can put all that in a txt-File with System.IO;

Comment
Add comment · Show 1 · 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 PultroN93 · Apr 17, 2019 at 09:18 PM 0
Share

Hello, Yeah, thanks, I know now that i can't save whole GameObject. I'm going to change my scripts so i can save the attributes/variables by the method with PlayerPref. Thank you for your time!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

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

Hard question about saving and loading 1 Answer

Problem whith save system,Problem whith save code 0 Answers

Can gameobject be exported as unity3d form? 0 Answers

How to save and load any data type? 1 Answer

Save Multiple GameObject with XML 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