Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 KloverGames · Mar 22, 2021 at 05:42 PM · errorvariablenullreferenceexceptionsave data

Save data not creating save file

I've done a save system before, and I didn't change anything about the system but it's not working. So here is my save script and it just creates a file and opens it when you want to load it. It keeps saying file not found and I'm getting a NullReferenceException


 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 using System.Runtime.CompilerServices;
 
 public static class SaveScript
 {
     public static void SaveStats(PlayerControls controls)
     {
         BinaryFormatter formatter = new BinaryFormatter();
         string path = Application.persistentDataPath + "/targetBlast.GameData";
         FileStream stream = new FileStream(path, FileMode.Create);
 
         PlayerData data = new PlayerData(controls);
 
         formatter.Serialize(stream, data);
         stream.Close();
     }
 
     public static PlayerData LoadStats()
     {
         string path = Application.persistentDataPath + "/targetBlast.GameData";
 
         if (File.Exists(path))
         {
             Debug.Log("File found" + path);
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream stream = new FileStream(path, FileMode.Open);
 
             PlayerData data = formatter.Deserialize(stream) as PlayerData;
             stream.Close();
 
             return data;
         }
         else 
         {
             Debug.Log("File not found" + path);
             return null;
         }
     }
 }


This is my main script where the variables that I'm saving are. I keep getting a NullReferenceException error saying Object reference not set to the instance of an object and I'm getting an error because of the variable "allPoints" for some reason.

 public void SaveGame()
 {
     SaveScript.SaveStats(this);
 }

 public void LoadGame()
 {
     PlayerData data = SaveScript.LoadStats();

     this.allPoints = data.allPoints;
 }

Here is my last script where I store the player's data

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class PlayerData
 {
     public int allPoints;
 
     public bool levelUnlockedZero;
     public bool levelUnlockedOne;
     public bool levelUnlockedTwo;
     public bool levelUnlockedThree;
     public bool levelUnlockedFour;
     public bool levelUnlockedFive;
     public bool levelUnlockedSix;
     public bool levelUnlockedSeven;
 
     public bool hasAbilityZero;
     public bool hasAbilityOne;
     public bool hasAbilityTwo;
     public bool hasAbilityThree;
     public bool hasAbilityFour;
     public bool hasAbilityFive;
     public bool hasAbilitySix;
     public bool hasAbilitySeven;
 
     public PlayerData (PlayerControls controls)
     {
         allPoints = controls.allPoints;
 
         levelUnlockedZero = controls.levelUnlocked[0];
         levelUnlockedOne = controls.levelUnlocked[1];
         levelUnlockedTwo = controls.levelUnlocked[2];
         levelUnlockedThree = controls.levelUnlocked[3];
         levelUnlockedFour = controls.levelUnlocked[4];
         levelUnlockedFive = controls.levelUnlocked[5];
         levelUnlockedSix = controls.levelUnlocked[6];
         levelUnlockedSeven = controls.levelUnlocked[7];
 
         hasAbilityZero = controls.hasAbility[0];
         hasAbilityOne = controls.hasAbility[1];
         hasAbilityTwo = controls.hasAbility[2];
         hasAbilityThree = controls.hasAbility[3];
         hasAbilityFour = controls.hasAbility[4];
         hasAbilityFive = controls.hasAbility[5];
         hasAbilitySix = controls.hasAbility[6];
         hasAbilitySeven = controls.hasAbility[7];
     }
 }
Comment
Add comment · Show 1
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 HellsHand · Mar 22, 2021 at 07:07 PM 0
Share

Could this be a BUG? I'm having the exact same problem. File saves perfectly fine in editor but not at runtime. Odd thing is I'm creating the folder the file goes in at the same time, the folder gets created but not the file.

Nope I'm just an idiot, forgot to load Resources at runtime.

1 Reply

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

Answer by DevManuel · Mar 23, 2021 at 01:10 PM

I don't know the answer, but I've already a working SaveManager, maybe it helps you:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using System.Xml;
 using System.Xml.Serialization;
 
 
 public class SaveManager : MonoBehaviour
 {
     
     public bool load = true;
     public bool save = false;
 
 
 
     public SaveData activeSave;
 
    
     
     void Awake() 
     {
         Debug.Log(Application.persistentDataPath);
         if(save){Save();}
         if(load){Load();}
     }
    
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.S))
         {
             Save();
         }
 
         if(Input.GetKeyDown(KeyCode.L))
         {
             Load();
         }
     }
 
     public void Save()
     {
         //print("saving");
         string dataPath = Application.persistentDataPath;
         //print(dataPath);
         
 
         XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
         FileStream stream = new FileStream(dataPath + "/" + activeSave.saveName + ".xml", FileMode.Create);
         //print(stream);
         serializer.Serialize(stream, activeSave);
         stream.Close();
 
         //print("saved");
     }
 
     
 
     public void Load() 
     {
         print("loading");
         string dataPath = Application.persistentDataPath;
         //print(dataPath);
 
         if(System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".xml"))
         {
             print("exsits");
             var serializer = new XmlSerializer(typeof(SaveData));
             var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".xml", FileMode.Open);
             //print(stream);
             activeSave = serializer.Deserialize(stream) as SaveData;
             //print(activeSave);
             stream.Close();
 
 
             
             
             
             
             
             print("loaded");
         }
     }
  /*
     //Load xml file
     public void Load()
     {
         string dataPath = Application.persistentDataPath;
         XmlSerializer serializer = new XmlSerializer(typeof(SaveData));            //Create serializer
         FileStream stream = new FileStream(dataPath, FileMode.Open); //Load file at this path
         activeSave = serializer.Deserialize(stream) as SaveData;
         stream.Close();//Close the stream
     }
 */
 
     public void LoadData()
     {
         string dataPath = Application.persistentDataPath;
         if (File.Exists(dataPath + "/" + activeSave.saveName + ".xml"))
         {
             //var serializer = new XmlSerializer(typeof(SaveData));
             //var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Create);
             
             //activeSave = serializer.Deserialize(stream) as SaveData;
             string filePath2 = dataPath + "/" + activeSave.saveName + ".xml";
 
             //string fileText = File.ReadAllText(filePath);
             XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
             using (StringReader reader = new StringReader(filePath2))
             {
                 //return (SaveData)(serializer.Deserialize(reader)) as SaveData;
                 activeSave = (SaveData)(serializer.Deserialize(reader)) as SaveData;
                 print("loaded");
             }
         }
         
     }
 
 }
 
 [System.Serializable]
 public class SaveData
 {
     public string saveName;
 
     public int age;
     public int mass;
     public int size;
     public int fitLevel;
     public bool tone;
     public bool softAP;
     public bool firstStart = true;
     public List<float> ListGraph1;
     public List<float> ListGraph2;
     public List<float> ListGraph3;
 }
 
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

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

159 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 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 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 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

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

accessing a variable from another script. 3 Answers

Problems creating a grid.I dont understand. 0 Answers

NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer


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