|
Hello Guys, I am trying to load prefabs (that reside in the resources folder) in a 2d array. The problem lies in this line:
Here is the entire code: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { }//end class The error I get is: "NullReferenceException: Object reference not set to an instance of an object" Would appreciate any feedback. Thanks
(comments are locked)
|
|
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);
}
}
}
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)
|
|
Thanks. I figured it out in C#. I wasn't dynamically creating the rows and didn't declare the 2d array properly.
1. public GameObject [][] pieces; 2. pieces = new GameObject[rows][]; 3.
(comments are locked)
|
|
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:
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.
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:
(comments are locked)
|

