Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by ugur_gok · Jul 01, 2019 at 06:30 PM · saveload

how can i save the gameobject to file and load it?

I created a gameobject at run time and i want to save this gameobject to file and after reload the scene. I want to load this gameobject with all component. Is it possible?

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
1

Answer by Phodex_Games · Jul 01, 2019 at 09:34 PM

Yes it is.

  1. Make sure the to make a prefab out of the object

  2. Save object code:

      public static class DataManager
         {
             public static GameObject SavedGameObject;
             string path = "Path/To/Save/Location";
         
             public static bool Save(GameObject obj)
             {
                     string data;
                     //Translate GameObject into path
                     string assetPath = AssetDatabase.GetAssetPath((GameObject)data);
                     if (assetPath == null) return false;
                     else data= assetPath;
         
                 //Encrypt Save Data (not nessecary, but nice to have)
                 BinaryFormatter formatter = new BinaryFormatter();
                 FileStream stream = new FileStream(path, FileMode.Create);
         
                 formatter.Serialize(stream, data);
         
                 stream.Close();
         
                 return true;
             }
         
              
             public static object Load()
             {
                 if (File.Exists(path))
                 {
                     BinaryFormatter formatter = new BinaryFormatter();
                     FileStream stream = new FileStream(path, FileMode.Open);
         
         
                     data = formatter.Deserialize(stream) as string
                     SavedGameObject = (GameObject)AssetDatabase.LoadAssetAtPath((string)data[key].Key, typeof(GameObject));
                    
                     stream.Close();
                 }
                 else
                 {
                     Debug.LogWarning("Save file not found.");
                     return null;
                 }
             }
         }
    
    
    
  3. The code stored the game object path as string and then loads the gameobject from the path again. The code is more for explanation and my not work right of. It is not tested, just edited some pieces of my own code and send it over to you.

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 ugurberkecanunlu · Jun 05, 2020 at 03:36 AM 0
Share

there are lots of bugs also can you explain more ?

avatar image Phodex_Games ugurberkecanunlu · Jun 05, 2020 at 06:08 AM 1
Share

Where exactly are bugs? Apart from that, this solution unfortunately doesn't work in a game build, because the AssetDatabase class is an editor only class. The way I do it atm is to have a serialized object, keeping track of my asset relative locations (at least the ones I need to save) and then use the Resource.Load<>() function, to load them, which can be used at runtime.

avatar image Omti1990 Phodex_Games · Mar 02, 2021 at 11:13 PM 0
Share

Um I think your function can't work? string data; //Translate GameObject into path string assetPath = AssetDatabase.GetAssetPath((GameObject)data); Your string "data" is empty and you try to cast your empty string as a GameObject? I don't understand what you're doing there...

avatar image
0

Answer by Batuhan13 · Jun 05, 2020 at 04:38 AM

Pls watch this video :) Tutorial video

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

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

109 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

Related Questions

Saving with PlayerPrefs and having multiple character slots? 5 Answers

Unity Web Player Account System 1 Answer

How to delete created Binary files 1 Answer

High Score with player prefs confusion (JAVA) 0 Answers

[Persistence] Saved To File, Not Updating 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