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 StubberDubber · May 11, 2015 at 07:36 AM · c#dictionarynull reference

Dictionary reference disappearing

Hi guys,

I'm pretty new to C#, and I've run into a bug thats probably an easy and simple fix but I'm just going round in circles now and can't see the solution.

I have a class 'Planet' which contains another class 'Planet_Data' (used for serialization) which contains another class 'Industry'. The Industry class has a Dictionary> member named 'productionSheet' (marked as [System.NonSerializable], constructed like so

 public Industry(Storage s)
 {

     productionUnits = new List<ProductionUnit>();
     ReloadDictionary();
 }

 public void ReloadDictionary()
 {
     productionSheet = new Dictionary<Enums.Commidities, ConsumptionAndProduction>();
     foreach (Enums.Commidities e in Enum.GetValues(typeof(Enums.Commidities)))
     {
         productionSheet.Add(e, new ConsumptionAndProduction(0, 0));
     }
 }

My planet object is constructed like so

 void Awake()
 {
     data = new Planet_data();

     // other members
     data.industry = new Industry(getStore());
     Debug.Log(data.industry.productionSheet);
     // other members
 }

When the scene is launched, the productionSheet is printed out correctly to the console, ie it is instantiated and assigned to at this stage. I then have a function which is called from Update(), which looks like this

 void Turn()
 {
     Debug.Log(data.industry);
     data.industry.ClearProductionSheet(); //
     //

 Industry::
 public void ClearProductionSheet()
 {
     foreach (var e in productionSheet) // null reference exception 
     {
         e.Value.consumption = 0;
         e.Value.production = 0;
     }
 }


The log shows that at the start of turn, the Industry object has been instantiated. However, the Dictionary contained within is now null.

I don't understand why my Dictionary has lost it's reference. Again, this is probably a simple thing I'm missing, but can anyone point out the problem to me?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 13, 2015 at 09:18 PM

Because unity saves data through XML serialization. Dictionaries are not serializeable. Lists and arrays are.

So, basically, you cannot save Dictionaries. You have to convert them to arrays or lists and back when saving/loading, or just use a list/array to begin with.

If you need to store the data in the dictionary, see this question and my comment in it to get you started on the conversion.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Changing material color from dictionary (C#) 0 Answers

How do I create a Key and Values Dictionary array in C# 1 Answer

Combine Children Dictionary in place of Hashtable? 2 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