- Home /
Item Database / Dictionary
So basically,what I'm trying to accomplish is create a dictionary and populate it so i can access the items to generate them.
using UnityEngine;
using System.Collections;
public class ItemClass : MonoBehaviour {
public string name;
public string description;
public int id;
public int uses;
public tk2dSpriteAnimator animations;
public ItemClass(string name,string description,int id,int uses,tk2dSpriteAnimator animations)
{
this.name = name;
this.description = description;
this.id = id;
this.uses = uses;
this.animations =animations;
}
}
public class ItemsDatabase : MonoBehaviour {
public Dictionary <string, ItemClass> dictionary = new Dictionary<string, ItemClass>();
public ItemClass metal = new ItemClass("Metal","Muy Duro,Brilla",0,1,null);
dictionary.Add(metal.name,metal);
}
the dictionary.Add(metal.name,metal); line fails,any insight?,thanks in advance.
Answer by Seregon · Jun 30, 2013 at 06:51 PM
Looking at the code posted, it looks like the dictionary.Add(metal.name,metal); line is inside the class definition, not inside a function. You can't run code inside a class definition, only define functions and member variables (in c# atleast, I think js is different). It doesn't look like theres anything wrong with the rest of the code, so the following should work:
public class ItemsDatabase : MonoBehaviour {
public Dictionary <string, ItemClass> dictionary = new Dictionary<string, ItemClass>();
public ItemClass metal = new ItemClass("Metal","Muy Duro,Brilla",0,1,null);
public void Start()
{
dictionary.Add(metal.name,metal);
}
}
You may or may not also want the dictionary and metal initialisations in the Start() function, but they will work equally well outside a function definition.
Thank,Im such a dumbass I didn't realize that, I also had to add using System.Collections.Generic; Cheers mate :)
No worries, it's easy to miss the obvious errors - I didn't spot the missing using statement either...