Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FritzPeace · Nov 04, 2015 at 08:24 PM · c#serializationexceptiondictionarysave-to-file

[Help] Dictionary bug

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<string, bool> playerHolder = new Dictionary<string, bool>();
  
      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<string, bool> playerSave;
  }
Comment
Add comment
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

2 Replies

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

Answer by jmorhart · Nov 04, 2015 at 11:05 PM

The NullReferenceException means that playerHolder is null. You are assigning playerHolder to point to data.playerSave, but data.playerSave has not yet been created. In your Load() method you should first check to see if the "test.dat" file exists, and if it doesn't call your Save() method first.

       public void Load()
       {
           if (!File.Exists(Application.persistentDataPath + "test.dat"))
           {
               Save();
           }
           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;
           }
       }
Comment
Add comment · Show 2 · 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
avatar image FritzPeace · Nov 05, 2015 at 11:31 AM 0
Share

I may be mistaken, but actually my code was kinda wrong. What I want to do, is to create the dictionary once, and then save it and load it back etc. public void OnEnable() { Load(); if (playerHolder != null) { playerHolder = new Dictionary (); playerHolder.Add("yellow", true); playerHolder.Add("fla$$anonymous$$go", true); playerHolder.Add("parrot", true); playerHolder.Add("pelican", true); playerHolder.Add("vulture", true); } Debug.Log(playerHolder); }

This shows better what I want. However now I am not able to save the dicionary at the end of the game to the dictionary inside the Serializable Class.

avatar image FritzPeace · Nov 05, 2015 at 11:36 AM 0
Share

I think I fixed it with this code, sorry for the wrong formatting :P Still Ill accept your answer, since its the only one! Thanks for the help.

avatar image
0

Answer by candy_man · Nov 05, 2015 at 01:04 PM

what is inside the variable name when it fails? I'm guessing the dictionnary does not contain a value for the name you are asking. Debug it to see it's value.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

does this key not found error have to do with serialization? 2 Answers

How to Serialize and Deserialize a Dictionary Containing Gameobject and a Vector 0 Answers

Better Dictionary? (serializable with more functionality) 1 Answer

Reference to value type 1 Answer

Nested Dictionary data lost after playmode 0 Answers


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