Unity3D/C#: Custom Lists Part 1

I am sure there’s a way to do this without coding in a system myself. I know the .NET Framework has a ‘list’ class and Unity3D probably has its own way of handling lists. But I quite liked the system I had in Dark Basic Professional, plus my system gives me the control that I want, unfortunately my system won’t appear in Unity3D’s editor but there is a work around to get it to work.

Inventory List

First we create a new script.


It will not be attached to anything in the editor, it is just where we’re going to create a new struct.

Clear the code in the file, all we’re doing is creating a new ‘struct’, so we don’t even need the script to have a class.

using UnityEngine;
using System.Collections;

public struct List
public string name;
public int count;


If you’re an ex DBP user this is the equivalent to:

type List

name as string

count as integer


If you’re not, then we’re just grouping together a set of variables/objects we wish to use later. At the moment, the struct is basic. All we need to know is the name of the item in the list and how many we have.

Then we can jump to the file where we want to store our list, mine’s in a ‘Stats.cs’ file and is attached to each player.

Here I create 3 public variables:

public static Stats Instance;

public List[] Inventory;
public string[] ItemNames;
public int[] ItemCounts;

Each of these is an array. Why have I done 2 separate arrays to our List one? Because these 2 will appear in the editor. You might already see a problem that may occur, what if our list items can hold lots of data? Wouldn’t we have to create more and more arrays? The answer is ‘no’, the solution should appear in ‘Part 2’. Anyway, this is my work around for it appearing in the editor. Instance is just for referring to this script from the outside.

In the same class, lets jump to the Awake() method (or create one if you need to)

Instance = this;

Inventory = new List[ItemNames.Length];
for (int i = 0; i < ItemNames.Length + 1; i++)
Inventory[i].name = ItemNames[i];
Inventory[i].count = ItemCounts[i];

Basically what’s happening is, we’re defining the array length of ‘List’ by matching it to the array length the user will define in the editor with ‘ItemNames’.

Then it checks every instance in the array and matches Inventory.name and Inventory.count to the editor’s variables.

This is very simple as you can see. But how would you display this list? Jump over to your GUI script and add the following variable:

private int h = 0;

You’ll see why ‘h’ is used below.

In the ‘OnGUI()’ method add the following:

h = 0;
for (int i = 0; i < Stats.Instance.Inventory.Length + 1; i++)
Helper.BorderTextBox(Stats.Instance.Inventory[i].name + ” x” + Stats.Instance.Inventory[i].count.ToString(), 300, 300 + h, 200, 100, Color.cyan);
h += 15;

Now, this is using my ‘ BorderTextBox’ function from this tutorial. But you can just replace it with GUI.Label(…) if you prefer.

What this is doing is checking each array slot inside of ‘Stats.Instance.Inventory’ (our Inventory List Struct) and displays the name and count for each of them. But instead of putting piling the labels on top of each other the next label will be ’15’ units lower, hence the use of ‘h’. ‘h’ has to be reset back to ‘0’ otherwise you’ll find your labels will scroll away as it’ll constantly increment.

And that’s it.

Here is how it should appear in the Unity3D Editor:

What would be ideal is if I could work out how to have the ‘count’ appear within ‘ItemNames’ for each element. However, this method still works.

So, how might I use an item?

Here is just a small example:

for (int i = 0; i < Stats.Instance.Inventory.Length + 1; i++)
if (Stats.Instance.Inventory[i].name == “Medicine”)
Stats.Instance.Inventory[i].count -= 1;


Every time you use this item, it’ll decrease in count. So it doesn’t matter which slot ‘Medicine’ is in.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s