This will be probably a trivial problem for an experienced programmer but I’m not one of these so here goes. Why the following piece of code throws NullReferenceException?
This script is attached to an empty object and ‘Bulb’ script is attached to a prefab. I’m essentially trying to dynamically create an array of ‘Bulbs’. Is it impossible to create new objects like that?
public class BulbCreator : MonoBehaviour {
public Transform Bulb;
public int width, height, spacer;
public Bulb[,] Bulbs;
void Start ()
{
Bulbs = new Bulb[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Bulbs[x,y] = Instantiate(Bulb, new Vector3(x*spacer, y*spacer, 0), Quaternion.identity) as Bulb;
//Debug.Log ("->"+b);
Bulbs[x,y].SetPos (x,y); <---- this line throws exception
}
}
}