- Home /
Best way to access static variables from the inspector
Ill start by by saying Im not sure if this is the best way to go about doing this, but its the best way I could come up with. So any suggestions on doing it a different way are welcome.
So, for my inventory, I have a class called Item with a bunch of variables (name and stock are the main two applicable here). To store them, I have created a static class name Inventory. In the constructor of Inventory I have created every item, with a stock starting out at zero. To add an item, I have a function that checks if the stock is less than one, if so I add it a generic list (Items.List) and add +1 to the stock. If the item's stock is greater than zero, I just add one to the stock. Inventory.Items becomes my active inventory, which is working like I want it to.
The problem I am running into is in my chests. I want them to give you some item from the Inventory class, and if the chest is locked it checks to see if an item (Inventory.key) has a stock of greater than zero. This is also working well also. What I want to do is make an option in the chest script that has a drop down menu where you can select the item to give, and select the item that unlocks it, so that I can change those two easily from the inspector from the same chest script.
I thought that creating an enum within the Inventory class might be a good way to go, but I don't how make the enum assign an item variable from the Inventory class to the chest class.
I would do a switch, but I have feeling inventory being static would give all kinds of problems with different chests that need different items assigned to them.
I just really want a field in my chest class where I choose from items that access them in the Inventory class. I thought about using a string var, but I don't know how to (or if I can) convert that string to a variable. For example, an item in the Inventory class is potion. to access it as an item variable i just script Inventory.potion. Is there a way to get stay an item variable that is Inventory."String", like make the string "Inventory.potion" and convert the string into the variable?
I also though about making separate scripts that I fed into the chest script, but I dont like having so many scripts attached to an object, and that would turn into alot of tiny scripts with all the chests and whatnot.
Any help or suggestions would be appreciated, thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SerializedProperty for Enum with EnumPopup 6 Answers
fftWindow isn't enum C#? 1 Answer
Very strange behavior after compiling for PC 0 Answers
CustomEditor EnumPopup using Images instead of enum values 0 Answers
KeyCode in custom editor 2 Answers