1

I've found some results on creating an array of arrays in c# but I'm unclear on how to do it with the Material type in Unity and where/if I should create new instances. I want an array called colorGroups and each element to have an array of Materials. I set the "pinks" and "blues" materials in the inspector. Maybe I shouldn't have the first array be the Materials type and GameObject instead?

Obviously this doesn't work but it's what I have so far...

    public Material[][] colorGroups;
    public Material[] pinks;
    public Material[] blues;
    void Start()
    {
        colorGroups[0] = pinks;
        colorGroups[1] = blues;
    }
tclarkMEOW
  • 131
  • 1
  • 12

1 Answers1

2

You have to initialize the array and define the length of the array. Else you are trying to reference something that does not exist yet.

void Start()
{
    // initializing colorGroups to be an array with a length of 2 in the 1st dimension
    this.colorGroups = new Material[2][];
    colorGroups[0] = pinks;
    colorGroups[1] = blues;
}

Now you can assign your Material array (blues) to your 2D array. You can check this answer to learn more about initializing 2D arrays in C#

KBaker
  • 401
  • 2
  • 7