Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

I dont get it, Data Not persisting between game loads

Discussion in 'Scripting' started by realdka13, Jun 23, 2018.

  1. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Im trying to set up a save for the users data. I set it up so that if there is no save file(Which means its the first load), it will credit the player with some items. But it does not seem to save, as it comes up as 0 the next time it runs. Anyone see what im doing wrong?

    The persistent script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.IO;
    7.  
    8. public class PersistentData : MonoBehaviour {
    9.  
    10.     public static PersistentData dataObject;
    11.  
    12.     //Persistent Data here
    13.     public int amps = 0;
    14.     //Capacitors
    15.     public int cap1v;
    16.     public int cap3v;
    17.     public int cap5v;
    18.     public int cap9v;
    19.     public int cap12v;
    20.     public int cap24v;
    21.     //Statistics
    22.     public int totalDistance;
    23.     public int lifetimeAmps;
    24.     public int totalCapsUsed;
    25.     public float lifetimeTimeInSeconds;
    26.     public int numberOfRuns;
    27.     public int distanceRecordNoCaps;
    28.     public int distanceRecordWithCaps;
    29.     public float highestAvgVoltage;
    30.     public float longestRunInSeconds;
    31.     public float highestAmpsEarned;
    32.  
    33.     //check if this object already exists and make sure its not destroyed every level
    34.     private void Awake()
    35.     {
    36.         if (dataObject == null)
    37.         {
    38.             DontDestroyOnLoad(gameObject);
    39.             dataObject = this;
    40.         }
    41.         else if (dataObject != this)
    42.         {
    43.             Destroy(gameObject);
    44.         }
    45.     }
    46.  
    47.     public void Save()
    48.     {
    49.         //setup for saving
    50.         BinaryFormatter bf = new BinaryFormatter();
    51.         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
    52.  
    53.         //Add the data here too
    54.         PlayerData data = new PlayerData();
    55.         data.amps = amps;
    56.  
    57.         data.cap1v = cap1v;
    58.         data.cap3v = cap3v;
    59.         data.cap5v = cap5v;
    60.         data.cap9v = cap9v;
    61.         data.cap12v = cap12v;
    62.         data.cap24v = cap24v;
    63.         //Statistics
    64.         data.totalDistance = totalDistance;
    65.         data.lifetimeAmps = lifetimeAmps;
    66.         data.totalCapsUsed = totalCapsUsed;
    67.         data.lifetimeTimeInSeconds = lifetimeTimeInSeconds;
    68.         data.numberOfRuns = numberOfRuns;
    69.         data.distanceRecordNoCaps = distanceRecordNoCaps;
    70.         data.distanceRecordWithCaps = distanceRecordWithCaps;
    71.         data.highestAvgVoltage = highestAvgVoltage;
    72.         data.longestRunInSeconds = longestRunInSeconds;
    73.         data.highestAmpsEarned = highestAmpsEarned;
    74.  
    75.     //actually writing the data
    76.     bf.Serialize(file, data);
    77.         file.Close();
    78.     }
    79.  
    80.     public void Load()
    81.     {
    82.         if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
    83.         {
    84.             print("Save File Found");
    85.             //setup for loading
    86.             BinaryFormatter bf = new BinaryFormatter();
    87.             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    88.             PlayerData data = (PlayerData)bf.Deserialize(file);
    89.  
    90.             //Data Here Too
    91.             amps = data.amps;
    92.  
    93.             data.cap1v = cap1v;
    94.             data.cap3v = cap3v;
    95.             data.cap5v = cap5v;
    96.             data.cap9v = cap9v;
    97.             data.cap12v = cap12v;
    98.             data.cap24v = cap24v;
    99.             //Statistics
    100.             data.totalDistance = totalDistance;
    101.             data.lifetimeAmps = lifetimeAmps;
    102.             data.totalCapsUsed = totalCapsUsed;
    103.             data.lifetimeTimeInSeconds = lifetimeTimeInSeconds;
    104.             data.numberOfRuns = numberOfRuns;
    105.             data.distanceRecordNoCaps = distanceRecordNoCaps;
    106.             data.distanceRecordWithCaps = distanceRecordWithCaps;
    107.             data.highestAvgVoltage = highestAvgVoltage;
    108.             data.longestRunInSeconds = longestRunInSeconds;
    109.             data.highestAmpsEarned = highestAmpsEarned;
    110.  
    111.             file.Close();
    112.         }
    113.         else if(!File.Exists(Application.persistentDataPath + "/playerInfo.dat")) //If no save file, default the settings
    114.         {
    115.             print("No Save File Found");
    116.             dataObject.cap1v = 3;
    117.             Save();
    118.         }
    119.     }
    120.  
    121.     private void Update()
    122.     {
    123.         print(dataObject.cap1v);
    124.     }
    125.  
    126. }
    127.  
    128. [Serializable]
    129. class PlayerData {
    130.  
    131.     //Persistent Data goes here
    132.     public int amps = 0;
    133.     //Capacitors
    134.     public int cap1v = 0;
    135.     public int cap3v = 0;
    136.     public int cap5v = 0;
    137.     public int cap9v = 0;
    138.     public int cap12v = 0;
    139.     public int cap24v = 0;
    140.     //Statistics
    141.     public int totalDistance;
    142.     public int lifetimeAmps;
    143.     public int totalCapsUsed;
    144.     public float lifetimeTimeInSeconds;
    145.     public int numberOfRuns;
    146.     public int distanceRecordNoCaps;
    147.     public int distanceRecordWithCaps;
    148.     public float highestAvgVoltage;
    149.     public float longestRunInSeconds;
    150.     public float highestAmpsEarned;
    151. }
    And the script im trying to access it with:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MainMenuUI : MonoBehaviour {
    7.  
    8.     public Text capText;
    9.     public Text ampText;
    10.  
    11.     private int capCount;
    12.     private int ampCount;
    13.  
    14.     private void Awake()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         PersistentData.dataObject.Load(); //Load data
    22.  
    23.         ampCount = PersistentData.dataObject.amps; //Load Correct Amp Count
    24.         LoadCapCount(); //Load the total Cap count
    25.  
    26.         ampText.text = ((int)ampCount).ToString(); //Set amp count on UI
    27.         capText.text = ((int)capCount).ToString(); //Set amp count on UI
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.     }
    33.  
    34.     void LoadCapCount()
    35.     {
    36.         capCount =
    37.         PersistentData.dataObject.cap1v +
    38.         PersistentData.dataObject.cap3v +
    39.         PersistentData.dataObject.cap5v +
    40.         PersistentData.dataObject.cap9v +
    41.         PersistentData.dataObject.cap12v +
    42.         PersistentData.dataObject.cap24v;
    43.     }
    44.  
    45. }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    At first glance everything should be working. Make sure you're actually calling .Save() method at all.

    Also, don't forget to wrap your serialization code in try / catch block, so that in case of failure it won't lock the file.
    Keyword is finally

    Also, use Path.Combine instead of + operator to avoid / \ issues in different operation system envionments.
     
  3. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    Turns out i just screwed up my load function. I did the exact same thing as on save, when it should be the opposite