Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

How to declare array of arrays that should be visible in inspector?

Discussion in 'Scripting' started by MSSK15, Oct 8, 2021.

  1. MSSK15

    MSSK15

    Joined:
    Mar 30, 2017
    Posts:
    19
    Hi, I want to create an array of array that should be visible in unity inspector. I have tried different methods by some googling and searching various forums. no errors shown but it is not visible in inspector. tried
    Code (CSharp):
    1. [SerializeField]
    2.     public Texture[][] texs;
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    MSSK15 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,533
    Unity does not support multidimensional or (like in your case) jagged arrays when it comes to serialization. However you can create an array / List of a class that contains another array / List of another type. So you can do

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class TextureRow
    4. {
    5.     public Texture[] textures;
    6. }
    7.  
    8. [SerializeField]
    9. public TextureRow[] texs;
    10.  
    This can be serialized by Unity. Of course to access a certain texture in your "grid" you have to do
    tex[col].textures[row]
    instead of
    tex[col][row]
    . Of course when filling this array from code you have to create the TextureRow classes yourself as well as the nested arrays. The inspector will initialize them for you when you edit it in the editor.
     
    MSSK15 likes this.
  4. MSSK15

    MSSK15

    Joined:
    Mar 30, 2017
    Posts:
    19
    Thank you very much. it worked. :)