Say I have 10 different types of grenades, I would like to create one variable, to allow me to set the number of grenades given from the inspector, I could create an array with 10 integers and set the number of grenades there, however it would be much easier for me if every element of the array (10 in total) had a name next to its value in the inspector so I could clearly see which grenade type am I assigning the value.

Is there any variable type or something that would allow me to do this? If so what is it? And how can I create what I want?

Thanks in advance!

If more information/ better explanation of some aspect is needed ask.


2 Answers 2


There are multiple methods to deal with this.

1) You could create an enum with custom values that you could refer to:

public enum Grenades { Standard = 5, Cluster = 10, }

You can then refer to it by int value = Grenades.Standard

2) You could use a dictionary to store values:

Dictionary<string, int> grenades= new Dictionary<string, int>();
grenades.add("standard", 5);
grenades.add("cluster", 10)

int value = grenades["standard"];

Note: You may be better off building one script for grenades and possibly using prefabs for each of the values.

Link to dictionary info https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx

Link to enum source https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

  • \$\begingroup\$ Does the enum method allow me to set the value of each grenade type in the Inspector? E.g. Say I want to make flash bang equal to 3, but set it from the Inspector \$\endgroup\$
    – John Smith
    Dec 20, 2016 at 19:42
  • \$\begingroup\$ No it doesn't, but DMGregory's advice should give you exactly what you need. \$\endgroup\$
    – DavidC
    Dec 20, 2016 at 19:57

It sounds like you want to associate a name with an inventory slot containing a count of grenades of that type, exposed so you can edit them in the Inspector.

In that case, I'd expand on Raeles's answer with a custom struct:

using UnityEngine;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {
   // Represents the different kinds of items we can carry.
   public enum ItemType {
       Empty, // First value is the default. 
       EMP    // (etc.)

   // Represents an inventory slot containing some count of some item.
   [System.Serializable] // This makes the struct visible in the Inspector.
   public struct Slot {
       public ItemType type;
       public int count;

   // Exposes a list of slots in the Inspector.
   public List<Slot> slots;

In the Unity Inspector, this gives you something like this:

Screenshot of Unity Inspector, showing list of slots where user can select an Item Type and Count

Some things to note:

  • Instead of an enum describing the item's type, your slot struct could instead hold a reference to the item's prefab, making it easy to spawn the right grenade when you want to throw it.

  • If your struct's first member variable is a string, then that string will be displayed instead of the "Element 1" labels, which can make it easier to navigate the list, as long as you remember to keep the name in synch with the contents. (Sometimes I make a little Editor script that enforces this automatically, and then hide the name field so I can't mess it up)

    (I don't recommend using string comparisons on the name to actually decide the item's gameplay, as this is error prone - you lose the built-in validation you get from an enumeration or prefab link)

  • To make this even prettier, you can implement your own custom editor or property drawer to customize the interface for your inventory.

  • \$\begingroup\$ So this will allow me to set the Count variable for each individual grenade type, right? E.g. I can set the amount of flash bangs given to 5 and the amount of smoke given to 1. \$\endgroup\$
    – John Smith
    Dec 21, 2016 at 15:20
  • \$\begingroup\$ Yes, exactly. You can even pack in more information into a custom struct if you like - see this related answer for a few other applications. \$\endgroup\$
    – DMGregory
    Dec 21, 2016 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .