Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

How do you make a fixed subsection of an array?

Discussion in 'Scripting' started by Joevonfo, Sep 13, 2017.

  1. Joevonfo

    Joevonfo

    Joined:
    Mar 31, 2015
    Posts:
    77
    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.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    2,429
    Last edited: Sep 13, 2017
  3. Joevonfo

    Joevonfo

    Joined:
    Mar 31, 2015
    Posts:
    77
    Thanks, but that seems a bit over my current abilities :)

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

    landon91235

    Joined:
    Nov 8, 2011
    Posts:
    1,549
    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.
     
    karl_jones likes this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,181
    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?
     
    Kiwasi likes this.
  6. Joevonfo

    Joevonfo

    Joined:
    Mar 31, 2015
    Posts:
    77
    Yeah I went on to do that after a while :)

    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 :D
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    14,419
    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.
     
    KelsoMRK likes this.
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,181
    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.
     
    Kiwasi likes this.