2D Array keeps coming up as Null

Hi, I’m trying to create a 2d board in unity using a 2d array, but for some reason when trying to add an object to the 2d array it still comes out as null.

public class BlockPlacementTest : MonoBehaviour {

	public GameObject floor;

	public int size;
	public int width, height;

	private int[,] goalCoords;
	private List<Room> goalRooms;

	private Room[,] rooms;
	// Use this for initialization
	void Start () {
		rooms = new Room[width,height];
	// Update is called once per frame
	void Update () {

	public void createDungeon(int g){
		goalRooms = new List<Room>();
		rooms = new Room[width,height];
		goalCoords = new int[g,2];

		for(int i =0; i < g; i++){
			int x = (int)(Random.Range(0,rooms.GetLength(0)));
			int y = (int)(Random.Range(0,rooms.GetLength(1)));
			//Makes sure there isn't any repeats
			while(rooms[x,y] != null){
				x = (int)(Random.Range(0,width));
				y  = (int)(Random.Range(0,height));
			goalCoords[i,0] = x;
			goalCoords[i,1] = y;
			print("X: "+x+" Y: "+y);
//			Room r = new Room(0,size,goalCoords[i,0]*size,goalCoords[i,1]*size);
//			r.createBlocks(true);
			goalRooms.Add(rooms[x,y] = new Room(0,size,goalCoords[i,0]*size,goalCoords[i,1]*size));

//		for( int i = 0; i < width; i++){
//			for(int j = 0; j < height; j++){
//				if(rooms[i,j] == null){
//					rooms[i,j] = new Room(1,size,i*size,j*size);
//				}
//			}
//		}

Room Code:

public class Room : MonoBehaviour {

	public GameObject block;

	private int type;
	private int size;
	private int x,y;
	private int w,h;
	private int numbOfBlocks;

	private int[,] blocks;

	public Room(int t, int s, int x, int y){
		type = t;
		size = s;
		this.x = x;
		this.y = y;
		w = 16;
		h = w;
		blocks = new int[size,size];

	private void createRoom(){
		if(type !=1)
			for(int c = 0; c< size; c++){
				int by = (int)(Random.Range(0,size));
				int bx = (int)(Random.Range(0,size));
				if(blocks[bx,by] != 1){
					blocks[bx,by] = 1;
			for( int i = 0; i < size; i++)
				for(int j = 0; j < size; j++)
					blocks[i,j] = 1;

	public void createBlocks(bool goal){
		for(int xb = 0; xb < blocks.GetLength(0); xb++){
			for(int yb = 0; yb < blocks.GetLength(1); yb++){
				if(blocks[xb,yb] == 1){
//					Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube),new Vector3(xb+x,0,yb+y), Quaternion.identity);
					var wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
//					wall.AddComponent(Rigidbody);
					wall.transform.position = new Vector3(xb+x,0,yb+y);
						wall.renderer.material.color = new Color(0,0,0);
						wall.renderer.material.color = new Color(1,1,1);
	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () {

	public int[,] getBlocks(){
		return blocks;

	public int getX(){
		return x;

	public int getY(){
		return y;

	public int getArrayX(){
		return x/size;

	public int getArrayY(){
		return y/size;

	public int getRoomType(){
		return type;

Any possible reasons why it keeps printing null?

Well, the answer is simple:

You created a MonoBehaviour with “new”!

You can’t create MonoBehaviours (or any kind of Component) manually. You always have to use AddComponent. Components can only “live” on GameObjects. If you create a MonoBehaviour with new, it’s missing it’s native part and it “fakes” itself to be null. The same happens when you destroy a component. As you might know in managed environments you usually can’t destroy objects. Objects are garbage collected when all references to that object are gone.

However components can be destroyed (they are actually C++ objects, the C# class is “just” the managed counterpart). When you Destroy() a component (or any other object derived from UnityEngine.Object) the native part get destroyed but the managed part will be there until it’s garbage collected once all references are gone. The managed object has an internal flag that it’s actually dead. The UnityEngine.Object has overridden the Equals and ==operator to fake the reference is null.

You shouldn’t implement a constructor on MonoBehaviour classes. Instead you can implement a create method like this:

public static Room Create(int t, int s, int x, int y)
    GameObject GO = new GameObject("Room_"+x+"_"+y);
    Room R = GO.Addcomponent<Room>();
    R.type = t;
    R.size = s;
    R.x = x;
    R.y = y;
    R.w = 16;
    R.h = w;
    return R;

this method will create a new GameObject with a Room script attached, will initialize your desired variables and return the reference to the Room component.