Hey there! I’m trying to work with a jagged array of game objects. I’ve been having troubles with this for a week now, I just can’t seem to get it; each thing I try results in errors. I think this is the most correct way I’ve tried though! Here’s the code, minus commented out attempts:

using UnityEngine;
using System.Collections;

public class Initialization : MonoBehaviour {

     public GameObject object01;
     public GameObject object02; //these 6 are
     public GameObject object03; //assigned
     public GameObject object04; //via the
     public GameObject object05; //inspector
     public GameObject object06;

     GameObject[][] jaggedTest;
    

     void Start () {    
          jaggedTest[0] = new GameObject[] {object01, object02, object03, object04, object05, object06};    
     }
}

The complete error is as follows:

NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
Initialization.Start () (at Assets/Scripts/Initialization.cs:136) //Line 17 in the above

The code is a tad stripped away for brevity, but the error remains identical.

I chose jagged arrays because I have 40 arrays of gameobjects of length 6 and 7 and I think it’s the fastest way to access them. My application is targeting mobile platforms and hinges around the access of these arrays, so performance of this is important.

You haven’t initialized jaggedTest.

    jaggedTest = new GameObject[1][]; //Replace 1 with the number you need