Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Serialized variable not showing in the Inspector?

Discussion in 'Scripting' started by pthfndr, Feb 17, 2021.

  1. pthfndr

    pthfndr

    Joined:
    Dec 7, 2020
    Posts:
    3
    I'm having some problems getting one of my variables displaying in the inspector and I don't know what the issue is. Any help is appreciated!


    Inspector
    upload_2021-2-17_0-12-20.png
    Inventory Class
    upload_2021-2-17_0-12-33.png

    Item Class
    upload_2021-2-17_0-12-52.png
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I don't think that the editor can display a List of Lists, just like it can't display a dictionary.
     
    pthfndr likes this.
  3. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    181
    It also can't serialize a stack or a queue, unfortunately.
     
    pthfndr likes this.
  4. pthfndr

    pthfndr

    Joined:
    Dec 7, 2020
    Posts:
    3
    Should I perhaps consider a different implementation for an inventory system then?
     
  5. jldooley1

    jldooley1

    Joined:
    Feb 15, 2020
    Posts:
    28
    Don't know if this will be useful for what you need, but you can serialize a list of classes. I've used it on occasion for holding mixes of properties in a list or array (say an Enemy Prefab, a Transform to Spawn at, and Health to give it)

    Code (CSharp):
    1.     public class TargetType : ScriptableObject
    2.     {
    3.         public List<DamageScenario> scenarios;
    4.  
    5.         public void OnDamageRecieved(Target target, WeaponProfile weaponProfile)
    6.         {
    7.             DamageType damageType = weaponProfile.damageType;
    8.  
    9.             if (scenarios.Exists(x => x.action == damageType))
    10.             {
    11.                 int i = scenarios.FindIndex(x => x.action == damageType);
    12.                 scenarios[i].reaction.Raise(target);
    13.             }
    14.         }
    15.     }
    16.  
    17.     [System.Serializable]
    18.     public class DamageScenario
    19.     {
    20.         public DamageType action;
    21.         public BaseDamageReaction reaction;
    22.     }
    upload_2021-2-17_19-33-32.png

    You might be able to use polymorphism to store all your different items under the one class.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What are you actually using the stack for? If it's to literally stack the same item, like 10 health potions, I would use a quantity field in the slot instead. That'll save you a lot of logic headaches peeking and popping stacks as you manipulate non-stackable items.

    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class InventorySlot
    4. {
    5.   public Item item;
    6.   public int quantity;
    7. }
    8.  
    Then use a List of InventorySlots instead of a a List of Stacks of Items. It'll show up in the inspector.
     
    pthfndr likes this.
  7. pthfndr

    pthfndr

    Joined:
    Dec 7, 2020
    Posts:
    3


    That was my intention yes. I've gone ahead and implemented your suggestion as it's much easier to work with. Thank you.
     
    GroZZleR likes this.