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
1
Question by colourcrisis · Aug 25, 2018 at 09:04 PM · serializationfilesaveloaddata storage

Saving/Loading gameobject name in .dat file

Hi guys!

I have 3 instantiated gameobjects with names I wish to save to a .dat file, so I can load them back at any point:

 gameobject1
 gameobject2
 gameobject3

All of which has the below script attached.

However, instead of saving all the names of the objects and placing them back, it only saves the last object's name and replaces that name to all the objects that has the script attached.

Here's the code I have so far:

 using UnityEngine;
 using System;
 using System.IO;
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public class saveFile : MonoBehaviour {
 
 
     [System.Serializable]
     class playerData
     {
         public string name;
     }
 
     public void Save()
     {
         BinaryFormatter binary = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath +"/SaveGame.dat"); 
         playerData dat = new playerData();
 
         dat.name = gameObject.transform.name;                      
 
         binary.Serialize(file, dat);
         file.Close();
     }
 
     void Awake()
     {
         if (File.Exists(Application.persistentDataPath + "/SaveGame.dat"))
         {
             BinaryFormatter binary = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
 
             playerData dat = (playerData)binary.Deserialize(file);
             file.Close();

             //this sets all the saved data back onto gameobject
             name = dat.name; 
         }
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by JDelekto · Aug 25, 2018 at 09:39 PM

The problem is that each object will be serialized in turn (apparently it is not asynchronous or you would have received errors during serialization).

As each object is serialized in turn, they are going to create a brand new "SaveGame.dat" file, and it will overwrite the file that the previous object had created. The last object to get serialized will then be the "winner" and when de-serialization, all objects will have the last object's name and properties. Recreating the file each time is just overwriting the last serialized object's properties.

You will want to find a different approach to serialize your data. One way to do this would be to put the actual serialization script not onto each object, but perhaps on a parent object or even some other object (like maybe a serialization manager) instead.

You can create a collection of objects which you can add the those you wish to serialize (you could use tags to select them as well), then have your save operation collect all of them and then save them out in one fell swoop.

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 colourcrisis · Aug 25, 2018 at 09:41 PM 0
Share

Thank you so much, this makes a lot of sense!

avatar image colourcrisis · Aug 25, 2018 at 10:11 PM 0
Share

I tried this, but no luck... nothing is being saved. "SerializationException: serializationStream supports seeking, but its length is 0."

 private GameObject[] entry;
 
 void Awake()
     {
         entry = GameObject.FindGameObjectsWithTag("itemCohort");
     }    
 
 public void Save()
     {
         BinaryFormatter binary = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath +"/SaveGame.dat"); 
         playerData dat = new playerData();
 
         for (int i = 0; i < entry.Length; i++) {
             dat.name = entry [i].name;
             binary.Serialize (file, dat);
         }
         file.Close ();
     }
avatar image JDelekto colourcrisis · Aug 25, 2018 at 10:40 PM 0
Share

Do you only have this script on one object?

avatar image colourcrisis JDelekto · Aug 25, 2018 at 10:48 PM 0
Share

Yep! On the parent object... Wondering what you thought

Show more comments

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

93 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

Related Questions

"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers

Saving gameobjects to .NET binary files 1 Answer

Binary Serialization (In Editor) - Path Access denied / File not found 0 Answers

Writing binary files on Android 1 Answer

android serialization weird path issue " IsolatedStorageException ", javascript 0 Answers


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