How show List with initial capacity in inspector?

The list size is 0 in editor,

public List<Weapon> weapon = new List<Weapon>(2);

Weapon is declared in other script,

[System.Serializable]
public class Weapon
{

    public string type;
    public float range;
    public float damage;
    public float coolDown;

}

you don’t have to allocate Lists :slight_smile:
you just do new List() to have a reference to a List instance of type Weapon. List are dynamic so you can List.Add, List.Remove, …

I know, but i want to edit the list variable one by one in unity editor before the project run.

The list hasn’t been populated, so it shows you the zero, or first, member of the list in the editor. Just click on the little arrow and fill it in, and you can add another.

The capacity is not the same thing as size, and has to do with pre-allocating memory. Normally you’ll never set the capacity, but there are cases where its useful.

To create a list with two weapons, you can write (I edited this to Timelog’s suggestion below):

List<Weapon> weapons = new List<Weapon> { new Weapon(), new Weapon() };

Or, if you want a solution that will take a number as the quantity (this can be useful if the size is a variable or constant), add this to the top of your script:

using System.Linq;

…and to declare your list:

List<Weapon> weapons = new List<Weapon>(Enumerable.Range(0, 2).Select(i => new Weapon()));

My initial instinct was to use Enumerable.Repeat, but this will only create one Weapon and assign it to both array indexes. It’s useable for value types though.

Finally, I noticed you named the list “weapon”. I really suggest naming it in the plural form of “weapons”.

imo the most elegant way to assign to a list at creation is the default way:

List<Weapon> weapons = new List<Weapon>
{
    new Weapon(),
    new Weapon()
};

That also allocates the least memory of all given options.

Heh, I somehow never realized you could do that. Now I know!

1 Like

hmm, a serialised class will show up in your list in the inspector, but won’t be editable.
If that’s what you want have a look into Scriptable Object. chances are you’ll have to make a custom editor/inspector window to create and edit an instance of your class.

That is incorrect. A serializable class is editable in the inspector even if it’s part of a list or array.

true! my bad, dunno why I thought they wouldn’t be :eyes:
anyway it isn’t all that clear to me what the OP actually wants :stuck_out_tongue:

Basically, if you declare something like: public int hest = 3; it shows up in the inspector with a value of 3 (which you can then change). Shitman wanted to declare a public list of Weapon, and have it by default show up in the inspector with a pair of Weapon before he made any changes to it. Given his code example, he mistook capacity for size (a mistake I also made the first time I tried to create a list with a specific size).

The solutions in my post (which Timelog corrected) will work.

I find out that it is not necessary to define list size in the script. I just need to change the element size from 0 to 2 in inspector. :eyes:

public List weapon = new List();

1 Like