Initialize fields from a Static List

Hi all

I’m trying to discover unity and work with it on my spare time.

I’m trying to get an inventory for my character.

So I’ve a static class with a static dictionary of kvp .

Item is a class I wrote that doesn’t inherit from monobehaviour, but from Component. I’ve other classes inheriting from Item (Consumable and Weapon for now).

I have an ItemController class that does inherit from MonoBehaviour. That class has a field “Item myItem”. In the Start() method of ItemController, I do this:

myItem = ItemList.itemList[gameObject.name]

(Obviously, I give the gameObject ItemController is attached to the name of the Item I hope to get.)

However, when I look in the inspector, the myItem field is always “None”.

I’ve tried different ways to trigger the instantiation but didn’t get anything satisfactory.

Any idea?

You can’t and shouldn’t derive a class from “Component”. Component is only ment as internal base class for the various component types which are actually defined on the native code side in C++. From the scripting environment (mono) you should only derive either from MonoBehaviour or from ScriptableObject (at least when it comes to runtime classes). Of course lately the modular extensions introduced some more like NetworkBehaviour and all the UI elements. However those are actually derived from MonoBehaviour.

The main counter question here is where and how do you actually populate that static dictionary?

There’s not enough information provided to fully answer your question.