- Home /
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
Anybody know how to do the same thing in JavaScript?
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);
}
}
}
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!
Answer by vib5252 · Sep 21, 2011 at 04:20 PM
Thanks. I figured it out in C#. I wasn't dynamically creating the rows and didn't declare the 2d array properly.
Declare 2D array
(Dynamically) Initialize the 2D array
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);
}
}
Answer by jahroy · Sep 21, 2011 at 04:05 AM
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:
- Use dynamic arrays to fill the arrays with data, then convert them to builtin arrays afterwards.
- 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.
- 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:
(Image deleted)