How to declare a list of arrays?
As the title says, is this possible?
If I had, for example:
public List <Weapon> wpn;
public List <Armor> amr;
How could I then create an array of these lists that will display nicely in the Inspector? Both weapon and armor inherit from Item
Answer by UnityCoach · Aug 16, 2017 at 05:59 PM
Lists and arrays display the same in the inspector.
But the type must be Serialisable. Make sure you use the [System.Serializable] attribute on them.
[System.Serializable]
public class Item {}
public class Weapon : Item {}
public class Armor : Item {}
Thank you for the reply. I have these lists set up and displaying correctly. I should have mentioned the main reason I would like an array of lists is to help serialise my inventory.
In the Inspector I would like an array called inventory of the lists above, and then I face the arduous task of serializing that array to a json file.
So what is the correct way to declare this array?
That's what I am currently using. But I'm currently writing each of these lists to JSON separately. I tried to serialise just one list but JsonUtility doesn't serialize derived classes as the correct type (always reverts to the parent type and data is lost as a result).
So my idea was to have separate lists for my different items, and add them to an array. That way I can (hopefully) serialize them at the same time, and also loop through them.
I've never used nested lists/arrays before, so I'm stuck at the first hurdle
Your answer
Follow this Question
Related Questions
[Quiz Game] How to prevent Question asked twice. HELP 1 Answer
Multiple Tags with FindGameObjectsWithTag...? 1 Answer
Need help to generate a random assortment of 4 buttons - C# 0 Answers
Unknown Argument Out of Range Index Error On Card Game 1 Answer
How to get all children of a Gameobject with a certain component 2 Answers