Hi, I am currently working on a project that involves a slot inventory system and everything about how the items being picked up/used/crafted etc, are identified is from a set of variables in a script on the object representing the item or icon representing it in the inventory. This is working great but the script I am storing them in is just a normal monobehaivour script with public variables that i fill in inspector. I am assuming that because the script is literally just holding some variables and not actually doing/running anything that there may be a better way to store the variables on the item objects. Also like mentioned I need to make these variables public which by my understanding is good to avoid if possible.
I usually make an xml file with the item structure in it, where I setup all my items.
And write some custom xml parser class to load in all my items at startup based on the xml file using Linq-to-xml, This also allows (mod support) in the future if people wanna add items of their own. Another good way is to use Scriptable Objects.
So, you have a MonoBehaviour that is just a set of variables? And do you have another MonoBehavior then that actually manages your inventory system and just gets the other scripts variables? You could always make just a basic class that doesn’t extend MonoBehaviour and then make a variable for that class in your main script. A struct could also do the same (similar to a class, but isn’t passed around by reference). If you need to be able to modify these variables, there are a few options. If you make a class or struct to hold these variables, you could add
[System.Serializable] above the class/struct to get it to show up in the inspector. Another cool way to go about this is to use ScriptableObjects. It’s a way to kind of make a prefab that holds configuration settings for something in your scene. Or that’s at least one example of how to use them. I also don’t think that just having a MonoBehaviour to hold some variables for inventory settings isn’t all that bad. Not sure what the BEST way to do it is, but those are some options.