x


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[i][j] = (GameObject) Instantiate (gems[i], 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[i].name);

    for (int i=0; i < rows; i++) {
       for (int j=0; j<cols; j++){
         pieces[i][j] = (GameObject) Instantiate (gems[i], 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

more ▼

asked Sep 21 '11 at 03:26 AM

vib5252 gravatar image

vib5252
16 1 2 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Anybody know how to do the same thing in JavaScript?

more ▼

answered Sep 27 '11 at 11:32 PM

IMTRIGGERHAPPY9 gravatar image

IMTRIGGERHAPPY9
132 29 43 44

I've not tested this, but at least it compiled ok:

public var spawn = 15;
public var rows = 10;
public var cols = 10;
public var gemObject: GameObject;
public var pieces: GameObject [,];
public var gems: Object [];

function Start () {
    //Initializing variables
    pieces = new GameObject[rows, cols];

    //load's gems from the resources Gem folder to this object array
    gems = Resources.LoadAll("Gems", GameObject);        

    for (i = 0; i < gems.Length; i++)
        Debug.Log("gems #" + i + "is: " + gems[i].name);

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++){
            pieces[i,j] = Instantiate (gems[i], Vector2(i,j), Quaternion.identity);
        }
    }
}
Sep 28 '11 at 12:18 AM aldonaletto

Thank you so much! after a months work i have finally gotten the code to work exactly how i want it. now i can move on to bigger and better things! i learned a ton about unity through this one code though!

Sep 28 '11 at 03:26 AM IMTRIGGERHAPPY9
(comments are locked)
10|3000 characters needed characters left

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

1. public GameObject [][] pieces;

2. pieces = new GameObject[rows][];

3.

    for (int i=0; i < rows; i++) {
       pieces[i] = new GameObject[rows];
       for (int j=0; j<cols; j++){
         pieces[i][j] = (GameObject) Instantiate (gems[i], new Vector2(i,j), Quaternion.identity);

       }
    }
more ▼

answered Sep 21 '11 at 04:20 PM

vib5252 gravatar image

vib5252
16 1 2 3

(comments are locked)
10|3000 characters needed characters left

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[i] = 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 + ")";
}


In my example I use an array of String arrays (rather than an array of GameObject arrays) but it shouldn't matter.

You can just replace generateObject(i, j) with a function (or code) that instantiates a GameObject.

The above example works for me:

alt text

more ▼

answered Sep 21 '11 at 04:05 AM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4150
x2084
x1358
x1034

asked: Sep 21 '11 at 03:26 AM

Seen: 6325 times

Last Updated: Sep 28 '11 at 03:26 AM