Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Binary Serialization for Instantiated Objects

Discussion in 'Scripting' started by unity_8g7XwYU6kpaB-Q, Sep 4, 2019.

  1. unity_8g7XwYU6kpaB-Q

    unity_8g7XwYU6kpaB-Q

    Joined:
    Aug 17, 2019
    Posts:
    9
    I am looking to save the position of the points in my game, nothing more. The player starts the game, if the player interacts with a point object, the point data is saved once saved triggers a hasBeenCalled bool, then when the player leaves and starts the game again, the hasBeenCalled bool stops CreatePoints() from being called and calls LoadPoints() instead. Can you give me any advice to get passed this? Thank you.

    Data.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Data {
    7.     ;
    8.     public float[] position;
    9.    
    10.     public Data (PointData point)
    11.     {
    12.         position = new float[3];
    13.         position[0] = point.transform.position.x;
    14.         position[1] = point.transform.position.y;
    15.         position[2] = point.transform.position.z;
    16.        
    17.     }
    18.  
    19. }
    SaveGame.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Runtime.Serialization.Formatters.Binary;
    4. public class SaveGame
    5. {
    6.    public static void SavePoints (PointData tile)
    7.    {
    8.        BinaryFormatter formatter = new BinaryFormatter();
    9.        string path = Application.persistentDataPath + "/tiles.info";
    10.        FileStream  stream = new FileStream(path, FileMode.Create);
    11.      
    12.        Data tileData = new Data(tile);
    13.      
    14.        formatter.Serialize(stream, tileData);
    15.      
    16.        stream.Close();
    17.    }
    18.    public static Data LoadPoints ()
    19.    {
    20.        string path = Application.persistentDataPath + "/tiles.info";
    21.        if (File.Exists(path))
    22.        {
    23.            BinaryFormatter formatter = new BinaryFormatter();
    24.            FileStream stream = new FileStream(path, FileMode.Open);
    25.          
    26.            Data tileData = formatter.Deserialize(stream) as Data;
    27.            stream.Close();
    28.          
    29.            return tileData;
    30.        }else{
    31.          
    32.            Debug.LogError("File path does not exist in this location: " + path);
    33.            return null;
    34.        }
    35.    }
    36. }
    37.  
    PointData.cs //each point game object has this script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PointData : MonoBehaviour
    6. {
    7.     public bool hasBeenCalled;
    8.     public GameObject pointTile;
    9.     public void SavePoints()
    10.     {
    11.         hasBeenCalled = true;
    12.         SaveGame.SavePoints(this);
    13.     }
    14.    
    15.     public void LoadPoints()
    16.     {
    17.  
    18.         Data data = SaveGame.LoadPoints();
    19.         Vector3 position;
    20.         position.x = data.position[0];
    21.         position.y = data.position[1];
    22.         position.z = data.position[2];
    23.         Instantiate(pointTile, position, Quaternion.identity);
    24.     }
    25. }
    26.  

    LevelHandler.cs // Only including the relevant code for this confusion.
    Code (CSharp):
    1. void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    2.     {
    3.         if (pointData.hasBeenCalled)
    4.         {
    5.             CreateLevel();
    6.             pointData.LoadPoints();
    7.             CreatePlayer();  
    8.         }
    9.         else{
    10.             CreateLevel();
    11.             CreatePoints();
    12.             CreatePlayer();  
    13.         }
    14.        
    15.     }
    Thank you for your time.