1
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
  • \$\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\$ – Vadim Tatarnikov Dec 20 '16 at 19:42
  • \$\begingroup\$ No it doesn't, but DMGregory's advice should give you exactly what you need. \$\endgroup\$ – Raeles Dec 20 '16 at 19:57
2
\$\begingroup\$

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. 
       Frag,
       Flashbang,
       Smoke,
       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 contains a string called "name" 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.

    (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.

\$\endgroup\$
  • \$\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\$ – Vadim Tatarnikov Dec 21 '16 at 15:20
  • \$\begingroup\$ @VadimTatarnikov 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 '16 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.