Search Unity

NullReferenceException with Static Persistent data

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

  1. realdka13

    realdka13

    Joined:
    Jun 19, 2018
    Posts:
    23
    I have a a static class PersistentData that im using to store all the game data to be reopened next time they play.

    That code:
    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.  
    15.     //check if this object already exists and make sure its not destroyed every level
    16.     private void Awake()
    17.     {
    18.         if (dataObject == null)
    19.         {
    20.             DontDestroyOnLoad(gameObject);
    21.             dataObject = this;
    22.         }
    23.         else if (dataObject != this)
    24.         {
    25.             Destroy(gameObject);
    26.         }
    27.     }
    28.  
    29.     public void Save()
    30.     {
    31.         //setup for saving
    32.         BinaryFormatter bf = new BinaryFormatter();
    33.         FileStream file = File.Create(Application.persistentDataPath + "/playerInfor.dat");
    34.  
    35.         //Add the data here too
    36.         PlayerData data = new PlayerData();
    37.         data.amps = amps;
    38.  
    39.         //actually writing the data
    40.         bf.Serialize(file, data);
    41.         file.Close();
    42.     }
    43.  
    44.     public void Load()
    45.     {
    46.         if (File.Exists(Application.persistentDataPath + "/playerInfor.dat"))
    47.         {
    48.             //setup for loading
    49.             BinaryFormatter bf = new BinaryFormatter();
    50.             FileStream file = File.Open(Application.persistentDataPath + "/playerInfor.dat", FileMode.Open);
    51.             PlayerData data = (PlayerData)bf.Deserialize(file);
    52.             file.Close();
    53.  
    54.             amps = data.amps;
    55.         }
    56.     }
    57. }
    58.  
    59. [Serializable]
    60. class PlayerData {
    61.  
    62.     //Persistent Data goes here
    63.     public int amps;
    64. }
    But i am trying to load the data on the main menu and its giving me a NullReference Exception on line 16

    That code:
    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.         PersistentData.dataObject.Load(); //Load data
    17.  
    18.         ampCount = PersistentData.dataObject.amps; //Load Correct Amp Count
    19.     }
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.         print(ampCount);
    24.         ampText.text = ((int)ampCount).ToString(); //Set amp count on UI
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.      
    30.     }
    31. }
    The Persistent Data object is a DontDestroyOnLoad as well.
    So how do i get that reference i need?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You're trying to set the value of dataObject in one script and access it in another in Awake. Since you don't know what order they are running, you are trying to access it before a value is assigned to it. Either move your access to Start so the Awake assigns first (generally the suggested method) or setup your script execution order so the PersistentData runs first.
     
    realdka13 likes this.