- Home /
The question is answered, right answer was accepted
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
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?
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! ;)
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.
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:
Create a single
Inventory
instance and store it somewhere persistent, then access only this instance.Or you can change
Inventory
to usestatic
members, negating the need for instance tracking.
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
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
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...
you can send the relevant files through dropbox or something if you like for debugging
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
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