I've been trying to set up my own class, GeneralItem, and store these in a Hashtable. The scripts are as follows:
GeneralItem class
using UnityEngine;
using System.Collections;
public class GeneralItem : Object {
public string itemName;
public int sellPrice;
public Texture icon;
public bool isStackable;
public string description;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
ItemList script
using UnityEngine;
using System.Collections;
public class ItemList : MonoBehaviour {
public GeneralItem Turnip, Potato;
private int amountOfItems;
public GeneralItem[] itemArray;
public Hashtable itemList = new Hashtable();
//Hashtable: key is what you search, value is what is returned
// Use this for initialization
void Awake () {
createItemList();
}
// Update is called once per frame
void Update () {
}
private void createItemList() {
Turnip = new GeneralItem();
Turnip.itemName = "Turnip";
Turnip.sellPrice = 50;
Turnip.icon = Resources.Load("icons/ItemIcon") as Texture;
Turnip.isStackable = true;
Turnip.description = "Just a turnip.";
itemList.Add(Turnip.itemName, Turnip);
Potato = new GeneralItem();
Potato.itemName = "Potato";
Potato.sellPrice = 60;
Potato.icon = Resources.Load("icons/PotatoIcon") as Texture;
Potato.isStackable = true;
Potato.description = "Only a potato.";
itemList.Add(Potato.itemName, Potato);
}
public GeneralItem FindItemByName(string itemName) {
if (itemList.ContainsKey(itemName)) {
GeneralItem newItem;
newItem = itemList["Turnip"] as GeneralItem;
return(newItem);
} else {
return null;
}
}
}
However, when trying to see what value comes with "Turnip" in the hashtable, I get 'GeneralItem' instead of 'Turnip'. Also, when I try to assign a GeneralItem via the interface of Unity, neither Turnip nor Potato shows up in the list of usable GeneralItems.
My question is, how can I make sure 'Turnip' is retrieved in the hashtable instead of 'GeneralItem'?