How do I add to a dictionary through the editor and then create gameobjects using that dictionary

Hi, so I am programming a 2D RPG in which I need to create items (obviously) and I have set up two generic scripts to define this.
Item.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Item
{
public Image artwork;
public new string name;
public string description;
public int price = 0;
public string damageText;
public string healthText;
public ItemType type;
public ItemRarity rarity;

public Item(Image artwork, string name, string description, int price, string damageText, string healthText, ItemType type, ItemRarity rarity)
{
this.artwork = artwork;
this.name = name;
this.description = description;
this.price = price;
this.damageText = damageText;
this.healthText = healthText;
this.type = type;
this.rarity = rarity;
} // Constructor
}

And then I also have Items.cs:

using System.Collections;
using System.Collections.Generic;

public class Items
{
private static Dictionary<int, Item> itemDictionary = new Dictionary<int, Item>();

public static void addItem(int id, Item item)
{
itemDictionary[id] = item;
}

public static Item getItem(int id)
{
return itemDictionary[id];
}
}

How would I make it so I can edit the Items dictionary and define new items and ids with that in the editor along with all of the stats listed in Item.cs?

The Unity inspector does not have any built-in ability to edit Dictionaries. You could create your own custom UI for doing this, but that’s probably not something you want to tackle if you’re new.

Instead, I suggest you consider using ScriptableObjects for your items. This will allow you to save each individual Item object as an asset in your project folder.

If you really need a Dictionary, you could then write a script that would load those items when your game starts and stick them into a Dictionary for reference.

By the way, if you’re going to post code, please use code tags.

1 Like

Please use code tags.

Unfortunately, Unity Editor’s Inspector currently does not support Dictionaries out of the box.

This may help, but I have never tried it.

1 Like

IIRC Serialization callback page in manual has an example, how you can setup a dictionary to show up in inspector. It uses 2 lists, one for keys and another for values.