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 · Jun 13, 2016 at 03:32 PM · savevariablesloadsystemstates

Problem with Loading States

I have a big problem with my Save System: I created some global scripts that collect multiple variables in Lists, like picked up items and opened or closed door. These global Lists are saved by a Save System script, here it is...

 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 glob;
     GameObject inv;
 
     Start_Scene startScript;
     Global_ItemInventory globItem;
     Global_Entities globEntity;
     Global_TriggerEvents globTrigger;
     Global_ScriptedEvents globEvent;
     Inventory_System invScript;
 
     public string savePoint;
 
     public IEnumerator waitLoadScene;
 
     private Scene scene;
 
     void Update()
     {
         player = GameObject.Find ("PlayerController");
         cam = GameObject.Find ("Camera");
         start = GameObject.Find ("Start Scene");
         glob = GameObject.Find ("GlobalParameters");
         inv = GameObject.Find ("Inventory");
 
         startScript = start.GetComponent<Start_Scene> ();
         globItem = glob.GetComponent <Global_ItemInventory> ();
         globEntity = glob.GetComponent <Global_Entities> ();
         globTrigger = glob.GetComponent <Global_TriggerEvents> ();
         globEvent = glob.GetComponent<Global_ScriptedEvents> ();
         invScript = inv.GetComponent<Inventory_System> ();
     }
 
     public void SaveData () 
     {
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
         PlayerData data = new PlayerData ();
 
         scene = SceneManager.GetActiveScene ();
         data.sceneName = scene.name;
 
         data.itemID = new int [globItem.itemSt.Count];
         data.itemState = new string [globItem.itemSt.Count];
         data.itemName = new string [globItem.itemSt.Count];
         for (int i = 0; i < globItem.itemSt.Count; i++) 
         {
             data.itemID [i] = globItem.itemSt [i].ID;
             data.itemState [i] = globItem.itemSt [i].State;
             data.itemName [i] = globItem.itemSt [i].Name;
         }
             
         globItem.SaveData ();
 
         data.maxInvSlots = invScript.maxSlots;
         data.slotID = new int[invScript.maxSlots];
         for (int i = 0; i < invScript.maxSlots; i++) 
         {
             data.slotID [i] = invScript.itemObj [i].ID;
         }
 
         invScript.SaveState ();
 
         data.doorID = new int [globEntity.doorSt.Count];
         data.doorState = new string [globEntity.doorSt.Count];
         data.lockState = new string [globEntity.doorSt.Count];
         data.doorName = new string [globEntity.doorSt.Count];
         for (int i = 0; i < globEntity.doorSt.Count; i++) 
         {
             data.doorID [i] = globEntity.doorSt [i].ID;
             data.doorState [i] = globEntity.doorSt [i].DoorState;
             data.lockState [i] = globEntity.doorSt [i].LockState;
             data.doorName [i] = globEntity.doorSt [i].Name;
         }
         data.slidoorID = new int [globEntity.slidedoorSt.Count];
         data.slidoorState = new string [globEntity.slidedoorSt.Count];
         data.slilockState = new string [globEntity.slidedoorSt.Count];
         data.slidoorName = new string [globEntity.slidedoorSt.Count];
         for (int i = 0; i < globEntity.slidedoorSt.Count; i++) 
         {
             data.slidoorID [i] = globEntity.slidedoorSt [i].ID;
             data.slidoorState [i] = globEntity.slidedoorSt [i].DoorState;
             data.slilockState [i] = globEntity.slidedoorSt [i].LockState;
             data.slidoorName [i] = globEntity.slidedoorSt [i].Name;
         }
 
         globEntity.SaveData ();
 
         data.triggerID = new int [globTrigger.trgDoorSt.Count];
         data.triggerState = new string [globTrigger.trgDoorSt.Count];
         data.triggerName = new string [globTrigger.trgDoorSt.Count];
         for (int i = 0; i < globTrigger.trgDoorSt.Count; i++) 
         {
             data.triggerID [i] = globTrigger.trgDoorSt [i].ID;
             data.triggerState [i] = globTrigger.trgDoorSt [i].State;
             data.triggerName [i] = globTrigger.trgDoorSt [i].Name;
         }
 
         globTrigger.SaveData ();
 
         data.eventName = new string [globEvent.eventSt.Count];
         data.eventState = new string[globEvent.eventSt.Count];
         for (int i = 0; i < globEvent.eventSt.Count; i++) 
         {
             data.eventName [i] = globEvent.eventSt [i].Name;
             data.eventState [i] = globEvent.eventSt [i].State;
         }
 
         globEvent.SaveData ();
 
         data.savePointName = savePoint;
 
         bf.Serialize (file, data);
         file.Close ();
     }
 
     public void LoadData()
     {
         if(File.Exists (Application.persistentDataPath + "/Data.dat"))
         {
             waitLoadScene = loading();
             StartCoroutine (waitLoadScene);
         }
     }
 
     IEnumerator loading ()
     {
         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);
         AsyncOperation async = SceneManager.LoadSceneAsync (data.sceneName);
 
         while (!async.isDone) 
         {
             yield return null;
         }
 
         startScript.LoadDataState (data.savePointName);
 
         globItem.LoadData (data.itemID, data.itemState, data.itemName);
 
         invScript.LoadData (data.maxInvSlots, data.slotID);
 
         globEntity.LoadData (data.doorID, data.doorState, data.lockState, data.doorName, data.slidoorID, data.slidoorState, data.slilockState, data.slidoorName);
 
         globTrigger.LoadData (data.triggerID, data.triggerState, data.triggerName);
 
         globEvent.LoadData (data.eventName, data.eventState);
 
         StopCoroutine (waitLoadScene);
     }
 }
 
 [Serializable]
 class PlayerData
 {
     public string sceneName;
 
     public string savePointName;
 
     public int[] doorID;
     public string[] doorState;
     public string[] lockState;
     public string[] doorName;
 
     public int[] slidoorID;
     public string[] slidoorState;
     public string[] slilockState;
     public string[] slidoorName;
 
     public int[] triggerID;
     public string[] triggerState;
     public string[] triggerName;
 
     public string[] eventName;
     public string[] eventState;
 
     public int[] itemID;
     public string[] itemState;
     public string[] itemName;
 
     public int maxInvSlots;
     public int[] slotID;
 }
 

The problem is that on load data, sometimes something is not correctly loaded (some item still spawns despite picked up, or some door still closed despite before opened). I believe that this problem popped up when I inserted more and more variables in save system, but i don't know.

Oh, i don't know if it can be useful, but the gameobject with save system script has got also DontDestroyOnLoad script.

I'm waiting for a solution.

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

0 Replies

· Add your reply
  • Sort: 

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

44 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

Related Questions

Hard question about saving and loading 1 Answer

Saving data 1 Answer

Getting error on instatiating. 0 Answers

Scene Saving 1 Answer

simple checkpoint/save/load system in javascript 4 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