How do you make a fixed subsection of an array?

Hi guys
I’m having a relatively small issue, that I for the life of me cannot figure out how to handle.

I want to make an array in which I can allocate stats on various units from the inspector.
That in itself seems relatively straight forward.
To give an example with 1 unit:

  • Size (1)
  • Name (string)
  • Health (int)
  • Attack (int)

I would however also like to be able to allocate traits like flying, invisible or regenerating to the units, and this is where things get a bit messy.
At this point the inspector looks like this:

  • Size (1)
  • Name (string)
  • Health (int)
  • Attack (int)
  • Traits (my script with traits in it)
  • Size (1)
  • Element 0
  • Flying (bool)
  • Invisible (bool)
  • Regeneration (script where the regeneration trait is divided into subsections)
  • Size (3)
  • Element 0
  • Regeneration 1 (bool)
  • Regeneration 2 (bool)
  • Regeneration 3 (bool)

When it would be a lot less messy and more effective if it looked like this:

  • Size (1)
  • Name (string)
  • Health (int)
  • Attack (int)
  • Traits (my script with traits in it)
  • Flying (bool)
  • Invisible (bool)
  • Regeneration (script where the regeneration trait is divided into subsections)
  • Regeneration 1 (bool)
  • Regeneration 2 (bool)
  • Regeneration 3 (bool)

I don’t know if it makes sense to you readers when put up like this, but is there a way to make subsections appear automatically in the size of 1, without showing it in the inspector, so that you get rid of the “size” and the “element 0” section in the inspector?

Thank you for reading.

Im not sure I fully understand but if you want to customise the inspector for a component you could just create a custom inspector or property drawer.

Thanks, but that seems a bit over my current abilities :slight_smile:

What basically looking for is a way to make sure that an array automatically has the size of one in the inspector, if possible?

You can initialize the array to be of size 1 by default, but you’re still going to have the “size” and “element 0” headers because it’s how Unity draws arrays in the inspector. If you wish to get rid of them, you’ll have to write a custom inspector.

1 Like

In reality, this doesn’t sound like a good use case for arrays to begin with. Why not just be declarative and put the properties you want in the thing you’re customizing?

1 Like

Yeah I went on to do that after a while :slight_smile:

But even so there are still some of the boolean values that would be nicer to have as a drop-down value in the inspector, so that every unit doesnt have +30 checkboxes open in the inspector, due to irrelevant traits for that unit specific unit.

It’s purely a cosmetic thing while programming though, so maybe it’s just one of those things I should learn to live with :smile:

Send the configuration data off to a prefab, scriptable object, or text file. Then you only need a single reference for each item in your array.

1 Like

This. Put all your traits in a SciptableObject and then let the unit reference just that. When you need a new one, just make a new SO instance and configure appropriately.

1 Like