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 bromley · May 25, 2016 at 09:45 AM · saveobjectsmultipleserialize

[SOLVED] Save System with Serialize multiple objects

I'm trying to create a complex save system that, when you save, serializes every data of the game.

I saw this tutorial: https://www.youtube.com/watch?v=dRDIiK7AGn4&ab_channel=CSharpAccentTutorials and it helps me so much for saving player position.

Now i want to do something else: i want save all doors state, so if a door is open and i save, when i load the game that door will be open. Anyway in the map there are more doors, and i'm trying to save all of them.

Obviously i don't want create one var for every doors in the game, so i want call them with GameObject.FindGameObjectsWithTag ("name of tag"). But i can't create only one var because this one will be the same for every door.

So my question is: how can i create multiple serialized strings and how can i call them, linking these with each respective door?

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 · May 25, 2016 at 10:42 AM 1
Share

You have to find a way to identfy each individual door. You could give each a unique ID, for example, ans use it to connect a door's state with the id, and after loading find a door and set it's state again.

Guid Structure

avatar image bromley Cherno · May 25, 2016 at 11:27 AM 0
Share

after doing this how can i call them? using for cycle?

avatar image Cherno bromley · May 25, 2016 at 11:09 PM 1
Share

You could write a custom $$anonymous$$onoB. class that holds the ID and open state, and add it to each door. Then when saving, collect all instances of this component, and use a dictionary to connect the ID with the state. You could create an instance of another custom class that holds this dictionary, and serialize it. When loading / deserializing, go through each component in the scene again and if the ID from the component and the dictionary key match, assign the corresponding state.

Show more comments

2 Replies

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

Answer by bromley · May 26, 2016 at 08:03 AM

I solve it, look the new script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine.SceneManagement;
 
 public class SaveSystem : MonoBehaviour {
 
     GameObject player;
     GameObject cam;
     GameObject start;
     GameObject[] doors;
     GameObject[] slideSDoors;
 
     Start_Scene startScript;
 
     private Scene scene;
 
     private int doorsCount;
     private int slideSDoorsCount;
     private int totalDoorsCount;
 
     void Start()
     {
         player = GameObject.Find ("PlayerController");
         cam = GameObject.Find ("Camera");
         start = GameObject.Find ("Start Scene");
         doors = GameObject.FindGameObjectsWithTag ("Door");
         slideSDoors = GameObject.FindGameObjectsWithTag ("SlideSecretDoor");
 
         startScript = start.GetComponent<Start_Scene> ();
 
         doorsCount = doors.Length;
         slideSDoorsCount = slideSDoors.Length;
         totalDoorsCount = doorsCount + slideSDoorsCount;
     }
 
     public void SaveData () 
     {
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
         PlayerData data = new PlayerData ();
 
         data.doorState = new string[totalDoorsCount];
         data.lockState = new string[totalDoorsCount];
 
         for (int i = 0; i <= totalDoorsCount; i++)
         {
             foreach (GameObject door in doors) 
             {
                 Rotating_Normal_Door doorScript = door.GetComponent<Rotating_Normal_Door> ();
 
                 if (doorScript.doorID == i) 
                 {
                     data.doorState [doorScript.doorID - 1] = doorScript.doorState;
                     data.lockState [doorScript.doorID - 1] = doorScript.lockState;
                 }
             } 
         }
 
         for (int i = 0; i <= totalDoorsCount; i++) 
         {
             foreach (GameObject slideSDoor in slideSDoors) 
             {
                 Sliding_Secret_Door slideSecretScript = slideSDoor.GetComponent<Sliding_Secret_Door> ();
 
                 if (slideSecretScript.doorID == i) 
                 {
                     data.doorState [slideSecretScript.doorID - 1] = slideSecretScript.doorState;
                     data.lockState [slideSecretScript.doorID - 1] = slideSecretScript.lockState;
                 }
             } 
         }
 
         scene = SceneManager.GetActiveScene ();
         data.sceneName = scene.name;
 
         data.posx = player.transform.position.x;
         data.posy = player.transform.position.y;
         data.posz = player.transform.position.z;
 
         data.rotx = cam.transform.localRotation.x;
         data.roty = cam.transform.localRotation.y;
         data.rotz = cam.transform.localRotation.z;
         data.rotw = cam.transform.localRotation.w;
 
         bf.Serialize (file, data);
         file.Close ();
     }
 
     public void LoadData()
     {
         if(File.Exists (Application.persistentDataPath + "/Data.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/Data.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize (file);
 
             file.Close ();
 
             PlayerPrefs.DeleteAll ();
 
             SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
 
             Vector3 newPlayerPos = new Vector3 (data.posx, data.posy, data.posz);
             Quaternion newPlayerRot = new Quaternion (0, data.roty, 0, data.rotw);
 
             startScript.LoadDataState (newPlayerPos, newPlayerRot);
 
             for (int i = 0; i <= totalDoorsCount; i++)
             {
                 foreach (GameObject door in doors) 
                 {
 
                     Rotating_Normal_Door doorScript = door.GetComponent<Rotating_Normal_Door> ();
 
                     if (doorScript.doorID == i) 
                     {
                         doorScript.LoadData (data.doorState [doorScript.doorID - 1], data.lockState [doorScript.doorID - 1]);
                     }
                 } 
             }
 
             for (int i = 0; i <= totalDoorsCount; i++)
             {
                 foreach (GameObject slideSDoor in slideSDoors) 
                 {
                     Sliding_Secret_Door slideSecretScript = slideSDoor.GetComponent<Sliding_Secret_Door> ();
 
                     if (slideSecretScript.doorID == i) 
                     {
                         slideSecretScript.LoadData (data.doorState [slideSecretScript.doorID - 1], data.lockState [slideSecretScript.doorID - 1]);
                     }
                 } 
             }
         }
     }
 }
 
 [Serializable]
 class PlayerData
 {
     public string sceneName;
 
     public float posx;
     public float posy;
     public float posz;
 
     public float rotx;
     public float roty;
     public float rotz;
     public float rotw;
 
     public string[] doorState;
     public string[] lockState;
 }
 

using the Arrays and adding different IDs for each door i solved my problem

thanks for support

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
0

Answer by Mmmpies · May 25, 2016 at 10:53 AM

Probably best to store them in an array if each door has a number you'd only need a boolean array to say if it's open or shut.

Save that array when you exit and load it when you startup and set the doors respectively.

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 bromley · May 25, 2016 at 11:35 AM 0
Share

this can be good using PlayerPrefs, but i want use an encrypted file where serialized values will be saved... i want to create a save system like "resident evil" where you need use a typewriter, so i think that i can't use your method

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

47 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

Related Questions

object list not serializing 2 Answers

SAVE OBJECTS IN ARRAY 1 Answer

Serialize large array of class 0 Answers

Moving multiple objects independenty on mobile using touchscreen 2 Answers

Delete a serialized binary save file 2 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