Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Inventory List Doesn't Display In Editor

Discussion in 'Scripting' started by jlpeyton, May 7, 2024.

  1. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    61
    Hello,

    When my player picks up an item and runs AddItem() I'm trying to have it added to my inventory list displayed within the gamemanager inspector. The item will show up for a brief second and then disappear though. I'm able to log the item quantity and as I pick up the same item it will increment.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.  
    3.     //Make instance of this script to be able reference from other scripts!
    4.     public static GameManager instance;
    5.  
    6. [Header("Items")]
    7.     public List<RPGItem> items;
    8.  
    9. [Header("Currently Owned Items")]
    10.     //View items that are in your inventory Can also be used to give the player some items to start the game with
    11.     public List<RPGItem> inventory;
    12.  
    13. void Start () {
    14.         instance = this;
    15.  
    16.         DontDestroyOnLoad(gameObject);
    17.     }
    18.  
    19.     //A method to add items to the inventory
    20.     public void AddItem(RPGItem item)
    21.     {
    22.         // Check if the item already exists in the list
    23.         foreach (RPGItem existingItem in inventory)
    24.         {
    25.             if (existingItem.itemName == item.itemName)
    26.             {
    27.                 // Item already exists, increment its quantity
    28.                 existingItem.quantity += item.quantity;
    29.                 return;
    30.             }
    31.         }
    32.  
    33.         // Item does not exist, add it to the list
    34.         inventory.Add(item);
    35.     }
    Code (CSharp):
    1. [CustomEditor(typeof(GameManager))]
    2. public class GameManagerEditor : Editor
    3. {
    4.     private GameManager gameManagerTarget;
    5.     private SerializedObject soTarget;
    6.  
    7.     //items
    8.     private SerializedProperty items;
    9. //inventory
    10.     private SerializedProperty inventory;
    11. private void OnEnable()
    12.     {
    13.         gameManagerTarget = (GameManager)target;
    14.         soTarget = new SerializedObject(target);
    15. //items
    16.         items = soTarget.FindProperty("items");
    17.  
    18.         //inventory
    19.         inventory = soTarget.FindProperty("inventory");
    20. }
    21.  
    22. public override void OnInspectorGUI()
    23.     {
    24.         soTarget.Update();
    25.         EditorGUI.BeginChangeCheck();
    26.  
    27.         gameManagerTarget.toolbar = GUILayout.Toolbar(gameManagerTarget.toolbar, new string[] { "Initialization", "Items", "Inventory", "Debugging" });
    28.         switch (gameManagerTarget.toolbar)
    29.         {
    30.             case 0:
    31.                 gameManagerTarget.currentTab = "Initialization";
    32.                 break;
    33.             case 1:
    34.                 gameManagerTarget.currentTab = "Items";
    35.                 break;
    36.             case 2:
    37.                 gameManagerTarget.currentTab = "Inventory";
    38.                 break;
    39.             case 3:
    40.                 gameManagerTarget.currentTab = "Debugging";
    41.                 break;
    42.         }
    43.  
    44. if (EditorGUI.EndChangeCheck())
    45.         {
    46.             soTarget.ApplyModifiedProperties();
    47.             GUI.FocusControl(null);
    48.         }
    49.  
    50.         EditorGUI.BeginChangeCheck();
    51.  
    52.         switch (gameManagerTarget.currentTab)
    53.         {
    54.             case "Initialization":
    55.                 EditorGUILayout.PropertyField(characterStatus);
    56.                 EditorGUILayout.PropertyField(characterSlots);
    57.                 break;
    58.             case "Items":
    59.                 EditorGUILayout.PropertyField(items);
    60.                 break;
    61.             case "Inventory":
    62.                 EditorGUILayout.PropertyField(inventory, true);
    63. break;
    64. }
    65. }
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,933
    What does the list show when this happens?
    If the item in the list says "Missing", that means something was destroyed.
    If the list reduces size, that means something was removed from the list.
     
  3. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    61
    This is one of those instances where I went to screenshot the issue and it suddenly started working...
    Consider this resolved I suppose.