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 Reconnoiter · Aug 17, 2016 at 02:50 PM · scenesaveloadsaveloadserialize

container class save based on Cherno's example

@Cherno

Howdy,

To create a serialize save-load system to store global variables and gameobjects (as in only the data that I need to save of the gameobjects) I followed one of Cherno's answers (atleast I remember it was from Cherno). Sadly I can't find the right link anymore.

But anyway from what I understand the example uses a container class for each gameobject and adds a list of which point to those containers classes in an other other main class which also stores global variables. This main class is serialized / saved.

The problem is that I still have some bug with linking the container classes and the main class. So it now only saves the global variables but not yet the gameobjects (the problem lies with the saving part since no matter how much gameobjects I add, the safe file remains tiny). Here is how main class and the container classes look like atm:

 [Serializable]
 public class SaveData { //stores all scene objects + global vars
     public List<SaveData_SceneObject> sceneObjects = new List<SaveData_SceneObject>(); 
 
     //Global vars
     public int testNum = 0;
 }
 
 [Serializable]
 public class SaveData_SceneObject { //container class
     public string name;
     public int id;
     public bool dontLoad;
     public float posX;
     public float posY;
     public float posZ;
     public float rotX;
     public float rotY;
     public float rotZ;
     public float rotW;
     public float scaleX;
     public float scaleY;
     public float scaleZ;
 }

Now to the save part (I think I am doing something wrong here or in the classes above):

 SaveData _data = new SaveData (); //class with global vars and lists of scene objects
 StoreSceneObjects (); //Store scene objects in classes (see below for details)
 SaveLoad.SaveToFile(_data, _savename); //this calls a function that serializes the data, but the serialize part is fine it is the _data that is wrong/lacking I think

And finally a StoreSceneObjects (); (please note the I add a debug.log at the bottom which shows that this function does run properly, also note that sceneObjectID is just a c# script that gives each object an ID number):

 public void StoreSceneObjects () {
         object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
         foreach (object o in obj) {
             GameObject g = (GameObject) o;
             sceneObjectID _idScript = g.GetComponent<sceneObjectID>();
             if (_idScript != null) {
                 SaveData_SceneObject _dataObject = new SaveData_SceneObject();
                 _dataObject.name = g.name;
                 _dataObject.id = _idScript.id;
                 _dataObject.dontLoad = _idScript.dontLoad;
 
                 _dataObject.posX = g.transform.position.x;
                 _dataObject.posY = g.transform.position.y;
                 _dataObject.posZ = g.transform.position.z;
 
                 _dataObject.rotX = g.transform.rotation.x;
                 _dataObject.rotY = g.transform.rotation.y;
                 _dataObject.rotZ = g.transform.rotation.z;
                 _dataObject.rotW = g.transform.rotation.w;
 
                 _dataObject.scaleX = g.transform.localScale.x;
                 _dataObject.scaleY = g.transform.localScale.y;
                 _dataObject.scaleZ = g.transform.localScale.z;
 
                 Debug.Log("GameObject saved: " + _dataObject.name);
             }
         }
     }

Thanks for taking the time!

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
1
Best Answer

Answer by Bunny83 · Aug 17, 2016 at 04:27 PM

Uhm, in line 7 of your last code fragment you create a new instance of your "SaveData_SceneObject" class, you fill in some information, but you don't do anything with that instance. I guess you want it to add to the "sceneObjects" list inside your "SaveData" class.

For this your "StoreSceneObjects" method should have a parameter of type "SaveData" so you can add the instances to the list

 public void StoreSceneObjects (SaveData data) {
     object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
     foreach (GameObject g in obj) {
         sceneObjectID _idScript = g.GetComponent<sceneObjectID>();
         if (_idScript != null) {
             SaveData_SceneObject _dataObject = new SaveData_SceneObject();
             // [ ... ]
             data.sceneObjects.Add(_dataObject);
         }
     }
 }

So obviously you have to call your method like this:

 SaveData _data = new SaveData ();
 StoreSceneObjects (_data);
 // [...]

If you take a look at your code, since _data is a local variable, how should "StoreSceneObjects" be able to add / modify anything inside your _data class?

Comment
Add comment · Show 2 · 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 Cherno · Aug 17, 2016 at 08:17 PM 0
Share

Buny83 is right, I think. You create a new instance of SaveData but don't do anything with it, while the StoreSceneObjects function stores GameObject data in a new instance of SaveData_SceneObject but again, nothing is done with these instances (they need to be returned or assigned to a static or passed SaveData instance).

Also check out my SerializeHelper, an extension of the method you refer to above.

avatar image Reconnoiter · Aug 18, 2016 at 12:13 PM 0
Share

Tnx it works, for people who are still trying to figure it out:

StoreSceneObjects (); needs to be StoreSceneObjects (_data); StoreSceneObjects () { ... } needs to be StoreSceneObjects (SaveData _data) { ... }

And add _data.sceneObjects.Add(_dataObject); like Bunny83 said.

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

56 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

Related Questions

Save and load a class 0 Answers

Save & Load Game question 3 Answers

Remembering the state of a scene 1 Answer

checkpoint level please 2 Answers

How can i save/load a changing variable in a scene file?(EX:highscore/name) 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