How to Serialize fields in class

hello,
I add items in game that have various effect. therefore needed to get list of all variables of characters
at first tried dictionary but they dont Serialize in inspector sadly,
after that make a swicth structure but there is a lot of item so it seems wrong
I have enums to select effect but still needed to paired with fields so still requires a lot labor
at last ı tried use reflections ı get values but could not set their value unity gives strange errors
finally ı idea came to my mind but ı guess this is imposible to like dictionaries but stil wanted to ask

my reflection try
enum numbers pairs with fields order ı use enums’ short values to find releated fields, all fields on inspector
Set() fuction is used by items through interface. system is working which Switch case but wanted to better solition


The error I get

You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

my last idea it a short list to hold number only is solves everthing but it make everthing too complicated with only numbers. The thing I want to put headers on top of each elemenst of list or Write headers instead of Element 0,1,2 etc. Is it posible ?

You definitely don’t need to use reflection for item effects. You can just compose them with scriptable objects, or using plain C# objects with [SerializeReference] potentially.

If you don’t know already, here’s the information about what Unity can and can’t serialise: Unity - Manual: Script serialization

1 Like