2D array of GameObjects C#

Hello Guys,

I am trying to load prefabs (that reside in the resources folder) in a 2d array. The problem lies in this line:

pieces_[j] = (GameObject) Instantiate (gems*, new Vector2(i,j), Quaternion.identity);*_
Here is the entire code:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {

* public int spawn;*
* public int rows;*
* public int cols;*
* public GameObject gemObject;*
* public GameObject [][] pieces;*
* public Object [] gems;*

* // Use this for initialization*
* void Start () {*

* //Initializing variables*
* rows = 10;*
* cols = 10;*
* spawn = 15;*
* pieces = new GameObject[rows][];*

//load’s gems from the resources Gem folder to this object array
*gems = Resources.LoadAll(“Gems”, typeof(GameObject)); *

* for (int i = 0; i < gems.Length; i++)*
_ Debug.Log(“gems #” + i + "is: " + gems*.name);*_

* for (int i=0; i < rows; i++) {*
* for (int j=0; j<cols; j++){*
pieces_[j] = (GameObject) Instantiate (gems*, new Vector2(i,j), Quaternion.identity);
}
}
}
}//end class*

The error I get is: “NullReferenceException: Object reference not set to an instance of an object”
Would appreciate any feedback.
Thanks_

Thanks. I figured it out in C#. I wasn’t dynamically creating the rows and didn’t declare the 2d array properly.

  1. Declare 2D array

  2. (Dynamically) Initialize the 2D array

  3. In the for loop (Dynamically) initialize the row and populate

public GameObject pieces;

pieces = new GameObject[rows];

	for (int i=0; i < rows; i++) {
		pieces *= new GameObject[rows];*
  •   	for (int j=0; j<cols; j++){*
    

pieces_[j] = (GameObject) Instantiate (gems*, new Vector2(i,j), Quaternion.identity);*_

* }*
* }*

The line of code that's causing problems appears to assume that you can dynamically resize a builtin array.

That is not allowed.

You never declare the size of the array before you start trying to populate it.

I'm sure there are many options, but I can think of three quickly:

  1. Use dynamic arrays to fill the arrays with data, then convert them to builtin arrays afterwards.
  2. Declare the size of your two-dimensional, builtin array before you try to fill it with data. I don't know how to do this. I think I tried for about two seconds one day then decided there were WAY better ways to accomplish my goal.
  3. Use a builtin array of dymanic arrays (see code below) to make it nice and simple.

You can read the documentation that describes the difference between builtin arrays and "javascript style" arrays here.

Here's some example code that uses a builtin array of dynamic arrays.

function populateArrayOfArrays ()
{
    var rows = 10;
    var cols = 20;

    /* declare a builtin array of 10 dynamic arrays */

    var twoDimArray : Array [] = new Array [rows]; 

    for ( var i = 0; i < rows; i ++ ) {

        /* create an new, empty array that can be resized */

        var temporaryDynamicArray = new Array();

        /* populate the temporary array */

        for ( var j = 0; j < cols; j ++ ) {
            temporaryDynamicArray.Push(generateObject(i, j));
        }

        twoDimArray *= temporaryDynamicArray;*
 *}*
 _/* test to see if it worked */_
 *for ( var r = 0; r < rows; r ++ ) {*
 *for ( var theItem : String in twoDimArray[r] ) {*
 *Debug.Log("Here's my story: " + theItem);*
 *}*
 *}*
*}*
*function generateObject ( theI, theJ )* 
*{*
 *return "String: (" + theI + ", " + theJ + ")";*
*}*
*```*
*<p>In my example I use an array of String arrays (rather than an array of GameObject arrays) but it shouldn't matter.</p>*
*<p>You can just replace <strong>generateObject(i, j)</strong> with a function (or code) that instantiates a GameObject.</p>*
*<p>The above example works for me:</p>*
*<p>(Image deleted)</p>*