Quantcast
Channel: Questions in topic: "exception"
Viewing all articles
Browse latest Browse all 470

[Help] Dictionary bug

$
0
0
Hey! I am working on a dictionary where I store bools, and then save them to a file. I got a bug, and despite my researches, haven't found any solution. If anyone can help me, Ill be glad! (the bug is the following: "Object Reference not set to an instance of an object" and the line it gives is the one with the comment saying Error is here, however I suspect the problem is somewhere in the start. using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class newText : MonoBehaviour { public static Dictionary playerHolder = new Dictionary(); public void OnEnable() { if(playerHolder.Count == 0) { playerHolder.Add("yellow", true); playerHolder.Add("flamingo", true); playerHolder.Add("parrot", true); playerHolder.Add("pelican", true); playerHolder.Add("vulture", true); } Load(); } public void OnDisable() { Save(); } public static void EnablePlayer(string name) { playerHolder[name] = true; } //Error is here... public static bool hasPlayer(string name) { return playerHolder[name]; } public void Save() { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "test.dat"); Data data = new Data(); data.playerSave = playerHolder; bf.Serialize(file, data); file.Close(); } public void Load() { if (File.Exists(Application.persistentDataPath + "test.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "test.dat", FileMode.Open); Data data = (Data)bf.Deserialize(file); file.Close(); playerHolder = data.playerSave; } } } [Serializable] class TestDataSave { public Dictionary playerSave; }

Viewing all articles
Browse latest Browse all 470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>