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 /
This question was closed Mar 16, 2014 at 06:07 PM by z3nth10n for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by z3nth10n · Dec 28, 2013 at 12:03 AM · errorobjectinventorysystem

Inventory System: "The given key was not present in the dictionary."

Hi,

well I have been making a Inventory System, and I have all time "The given key was not present in the dictionary." error, when I test it:

 using UnityEngine;
 using System.Collections;
 
 //ItemDataBase
 
 public class ItemScript : MonoBehaviour {
 
     private GameObject LoadItemPrefab(string name) {
         return (GameObject)Resources.Load ("prefabs/Items/"+name);
     }
     
     private Texture LoadItemTex(string name) {
         return (Texture)Resources.Load ("textures/Items/"+name);
     }
 
     void Start() {
 
         Inventory inv = new Inventory();
         
         InventoryItem preset;
         
         preset = new InventoryItem();
         preset.id = 1;
         preset.itemname = "Ring2";
         preset.DisplayName = "Ring of Gods";
         preset.itemtex = LoadItemTex (preset.itemname);
         preset.worldObject = LoadItemPrefab (preset.itemname);
         inv.AddNewItem(preset, preset.itemname); //Here it's assumed that is added a new value to the Dictionaru when that Script loads (that script is before than "GameGUI" that request that code.
 
     }
 
 }
 
 //Item Class
 
 public class InventoryItem
 {
     public GameObject worldObject;
     public int id;
     public string itemname;
     public string DisplayName;
     public Texture itemtex;
     public string itemtype;
     public string equipmenttype;
     public string usable;
     public float itemweight;
     public bool droppable;
     public Transform itemmodel;
     
     public int itemstacksize;
     public int itemstacklimit;
     public bool showStack;
     
     public int bagsize;
     public bool showBag;
     public InventoryItem[] BagItem;
 }

 public class Inventory {
 
        private Dictionary<string, InventoryItem> itemsBase = new Dictionary<string, InventoryItem>();

 public void AddNewItem(InventoryItem item, string name) {

     itemsBase.Add (name, item);

 }

     public InventoryItem FindItem(string name) {

     return itemsBase[name];

 }
 

 }
 
 public class GameGUI : MonoBehaviour {
 
 public void Start() {
 
  InventoryItem[] InventorySlots = new InventoryItem[5];
  
  InventorySlots[3] = FindItem("Ring2"); //There is now the error: "The given key was not present in the dictionary."
 
 }
 
 }

Function "Find" gives me a that error, and I don't know what can I do.... Where is my error?

Can somebody checks my code?

Thanks in advance. Bye.

I edited the code now I use a Dictionary

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

  • Sort: 
avatar image
0
Best Answer

Answer by KellyThomas · Dec 28, 2013 at 06:49 PM

Some other part of your code must be making bad requests of your inventory system.

If you add something like this it may help you track down the problem.

 public InventoryItem FindItem(string name) {

     if (!itemsBase.ContainsKey(name)){
         Debug.LogError("Unknown item name: " + name);
         return null;
     }

     return itemsBase[name];
 }


Two other questions / observations:

In ItemDataBase.Start() you declare and initialize an Inventory as a function variable. Don't you want this Inventory to persist beyond this function?

Where is the FindItem() function you access in GameGUI.Start() coming from?

Comment
Add comment · Show 6 · 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 z3nth10n · Dec 28, 2013 at 07:16 PM 0
Share

Well FindItem was tested in the same class (Inventory) but I misstyped it, and I don't put a Inventory inv = new Inventory(); & inv.FindItem("Ring2");


About the Start function, why I will want that? If I add the values to the dictionary, why do you say that? I don't understand, please try to explain better :P Sorry... But thanks for the response! ;)

avatar image KellyThomas · Dec 28, 2013 at 07:31 PM 0
Share

It sounds like you are creating a new Inventory each time.

Each new Inventory will be missing the contents of any previous Inventory.

You will either have to create a single instance and store it for later access or implement it using static elements.

avatar image z3nth10n · Dec 28, 2013 at 07:59 PM 0
Share

But if Start function is called only one time??

avatar image KellyThomas · Dec 28, 2013 at 08:14 PM 0
Share

Two of your classes have a start function I'm not sure which one you are referring to.

I think from your comments above that your GUI class is:

 public class GameGUI : $$anonymous$$onoBehaviour {
     public void Start() {
         InventoryItem[] InventorySlots = new InventoryItem[5];
         Inventory inv = new Inventory()
         InventorySlots[3] = inv.FindItem("Ring2"); //There is now the error: "The given key was not present in the dictionary."
     }
 }

What this means is that you are creating a brand new instance of Inventory which will have a brand new (empty) Dictionary. Then when you try to retrieve a value from this (empty) Dictionary it does not recognise your key ("Ring2").

The solutions are:

  1. Create a single Inventory instance and store it somewhere persistent, then access only this instance.

  2. Or you can change Inventory to use static members, negating the need for instance tracking.

avatar image z3nth10n · Dec 28, 2013 at 09:00 PM 0
Share

Off: I had finished my player system so, now I can write more frequently...


Do you have skype? Can I add you? We can solve that more quickly... :P


And where is a persistent area? :P

Show more comments
avatar image
0

Answer by sparkzbarca · Dec 28, 2013 at 12:16 AM

just means

items.Find ( delegate(InventoryItem Fitem) { return Fitem.itemname == "Ring2"; } ); this code presumably did not in fact find what it was send to look for

you could actually

Debug.log(items.Find ( delegate(InventoryItem Fitem) { return Fitem.itemname == "Ring2"; } ))

and that should return null as well

so now you have to find out why find isnt finding it

i can see that ring2 appears to be added to inv object

i dont see where you add it to items so that it can be found in it of course you may be doing that but based on your code you should be typing

inv.find(...)

not

items.find

Comment
Add comment · Show 3 · 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 z3nth10n · Dec 28, 2013 at 01:51 PM 0
Share

Well I will update the code, now I will use a Dictionary, and the same will happens... And I will try to explain how I do that...

avatar image sparkzbarca · Dec 28, 2013 at 06:39 PM 0
Share

you can send the relevant files through dropbox or something if you like for debugging

avatar image z3nth10n · Dec 28, 2013 at 07:28 PM 0
Share

Do you want test my project? :3 I have it in Dropbox give me your Dropbox's email and I will send you the folder via shared folder :P

Follow this Question

Answers Answers and Comments

20 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

Related Questions

inventory system issues 0 Answers

DontDestroyOnLoad with object on another scene 2 Answers

Object reference not set to... 1 Answer

An object reference is required to access non-static member `UnityEngine.Component.transform' 1 Answer

`System.IO.File' does not contain a definition for `ReadAllBytes'? 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