SIMPLE displaying static variables workaround

just sharing this as i didnt see anyone mention this when I was looking for work around and this is something I randomly stumbled apon like a few minutes ago. as we all know unity static variables are all hidden in the inspector. well I found a very very simple work around to display it that requires 1 empty gameobject and 1 script or more depending on if you want organization or something.

but here you go

public class inventorymanager : MonoBehaviour
{
   public static List<itemmold> inventory = new List<itemmold>(); // static variable I want to show
   public static int test; // static variable to display
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class display : MonoBehaviour
{
    public List<itemmold> inventorydisplay = inventorymanager.inventory;
    public int testt =inventorymanager.test;
}

ya that’s seriously it.
now you are able to actually edit what the value of the variables are through this as well.
so its not just a random instance of it.

for the list I was able to add a few scriptable objects in there and it updated all the calls to the list I made. so no need to have a dozen lists instances just this one updated all.

someone may have posted this before but I personally couldn’t find anything on this topic so enjoy

This is cute but it is definitely an abuse of the Unity serialization system.

I can sorta see how it might work but I can imagine a million ways it might fail in very subtle or even spectacular ways.

It may continue working indefinitely but I wouldn’t count on it, especially if you contemplate making a load / save function for your game.

Instead, stop using static variables inappropriately. :slight_smile:

The pattern you’re implementing above looks like some kind of master data repository for an inventory. You can do those without declaring their guts public and static.

Otherwise if you continue this way you may encounter some nasty surprises in the future when stuff mysteriously misbehaves or resets itself randomly.

These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

Inventories / shop systems / character selectors all contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation
    → what it looks like lying around in the world? In a chest? On a shelf?
    → what it looks like in the inventory window itself?
    → what it looks like when worn by the player? Does it affect vision (binoculars, etc.)
    → what it looks like when used, destroyed, consumed?

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.

Various possible inventory data structures in Unity3D:

that is alot of information that i didn’t really have a good grasp of before hand as I’m only like a year into coding. its a pretty simple mobile game I like to say but it really is pretty complicated to do. i chose to use static for the simplicity of it, Im fine to change it later but for now it is doing what it intended to do. like my inventory function really serves a few purposes. 1 there will be a reward system in the game that will add to the inventory, a inventory button ofc which just displays everything in alphabetical order and will soon have categories when I get that to work. a blacksmith to be able to craft things out of specifically materials, which is the main reason I made the main inventory static actually. as I couldn’t get the populated list from one class to the other. I do have a stacking function in place but its not the best as currently I cant think of a better method without going to tutorials. as currently the function is on the scriptable object which doesn’t reset every run which I feel like will be an issue later. I haven’t gotten the save feature done yet. as I wanted to focus on the blacksmith then do it as I watched a tutorial for the inventory and was using what I learned there to make the blacksmith myself and it was going pretty good. there’s a lot more to it but its an overwhelming amount when your not making the game with me lol. but ya I appreciate the heads up and will look into using my inventory more efficiently.