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 malkere · Oct 30, 2014 at 01:09 AM · nullreferenceexceptionstaticdictionary

Static Dictionary not initializing properly

For a mining game I have a public static dictionary that I was hoping to use to sort for calling by id, but I can't get it to call values from the dictionary. If I call a debug.log(dictionary.count.tostring()) it shows me that it is in fact 16 long, but all the values are null. I don't understand, and am currently calling item specifically, but this will make it very hard for me to call random items from a pool down the road.

 public static class Resources {
 
     public static Dictionary<int, Item> ResourceDictionary = new Dictionary<int, Item> {
         {0, null},
         {1, Resources.RawOre},
         {2, Resources.Gem},
         //etc
         };
     }
 
 
 PlayerScript.AddItem(Resources.RawOre, 10);
 //^^^^this works fine, calling directly
 for (int i = 0; i < newGems; i++){
     int gemType = Random.Range (10,15);
     PlayerScript.AddItem(Resources.ResourceDictionary[gemType], 1);
 //calling through the dict returns null reference exception
 }
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
1

Answer by Habitablaba · Oct 30, 2014 at 01:20 AM

Oh man, are Resources.Gem and the others stored as objects in the Resources class?
If so, you may be going about this the wrong way (or a less than ideal way, because who is to say what is right or wrong).

You want to make sure you are instantiating those objects as well, in order to add them to the dictionary (otherwise you'll end up with 16 valid keys with 'null' as the value since that is the value of those objects before they are instantiated).

Really all you want to store in that dictionary is a type, as far as I can tell. Or some way to know which type of resource to add to another list later. Instead of keeping them as objects, perhaps you could create an enum

 public enum Item
 {
   None = 0,
   Rock,
   Gem,
   Etc...
 }

You'd have to drop the 'null' for Item.None, but for the most part your code could stay.

Comment
Add comment · Show 4 · 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 malkere · Oct 30, 2014 at 03:21 AM 0
Share

yes it has 16, 10-15 were the predeter$$anonymous$$ed gem types i made, ruby, sapphire etc. It showed the count of the Dictionary as properly 16, as if it were initizliaed, yet all key wuld only return null. I setup a checker to post if for i<16 dict[i] == null, debug.log(i.tostring() + " is null"); and it showed them all empty.

I don't understand how it would get a proper count if all values were null.

avatar image Habitablaba · Oct 30, 2014 at 04:41 AM 0
Share

Here's a question: What is Item? and what are Resources.RawOre and Resources.Gem.

I had thought they were an enum, but I'm starting to question that.

avatar image malkere · Oct 30, 2014 at 06:09 AM 0
Share

ya i was pretty general with everything sorry.

Item is a static class with int _id, string _name, string _description.

Resources are a static hard coded Item.

I was thinking, rather than setting up a Dictionary to try and access items by a preset number I could just hard code a public static Item FindItemByID (int id) { if (id == 1) return Resources.RawOre; else if (id == 2) return Resources.RawGem;

etc....

but that doesn't sound very performance smart... an indexed object sounded much better but I couldn't get lists or dictionaries to work... frankly an array would work fine as I could just set the length as a variable and add to it whenever I add new items.... ?

still don't understand why I can't use a Dict like that though.

avatar image malkere · Oct 30, 2014 at 02:19 PM 0
Share

for example, I understand I could make a script separate and attach it to my camera and have it populate the static dictionary when the camera script loads, but is there no way to have the values just inherently static?

I was able to successfully build and int based return system, which is pretty much the same things, I just can't use the dictionary functions.

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

27 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

Related Questions

Allocating Game Object name dynamically using Find 0 Answers

What's the deal with the ..cctor() error? 2 Answers

should class members ever be static? 1 Answer

NullReference Problem with pragma strict 1 Answer

[Answered] NullReference problem with dictionary 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