Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    86
    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,813
    Last edited: Sep 13, 2017
  3. Joevonfo

    Joevonfo

    Joined:
    Mar 31, 2015
    Posts:
    86
    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. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,570
    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,438
    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:
    86
    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:
    15,033
    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,438
    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.