Skip to main content
Correcting mis-statement
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

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 containsstruct's first member variable is 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. (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)

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

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

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.

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

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

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.