Instantiating GameObjects From an array gives Null Reference Exception please help

Hello,

I am trying to instantiate a grid of gameObjects with the type of game object being stored in an array which is populated at runtime. The code runs fine when on its own (the top code). When I remove extends monobehavior and function call and set as an object in a different script (the bottom code). I get:

“NullReferenceException: Object reference not set to an instance of an object
MapMaker.GenerateMap () (at Assets/Script/BoardScripts/MapMaker.cs:136)
MapMaker.SetBoard (Int32 size, Int32 numberPlayers) (at Assets/Script/BoardScripts/MapMaker.cs:127)
MapTest.Start () (at Assets/Script/BoardScripts/MapTest.cs:8)”

I am by no means an expert on unity or c# and am I’m very confused. Any explanation as to why it stops working would be very helpful.

Code that works

using UnityEngine;
using System.Collections;

public class MapMaker : MonoBehaviour {
	public GameObject[] visuals;
	public int[,] board;
	
	void Start(){
		visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
		visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
		SetBoard (13, 4);
	}

	public void SetBoard(int size, int numberPlayers){
		board = new int[size, size];
		switch (size) {
		case 11://3*3
			switch (numberPlayers) {
			case 2:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				break;
			case 3:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				board [10, 5] = 9;
				break;
			case 4:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				board [10, 5] = 9;
				board [0, 5] = 10;
				break;
			}
			break;
		case 13://4*4
			switch (numberPlayers) {
			case 2:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				break;
			case 3:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				board [12, 6] = 9;
				break;
			case 4:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				board [12, 6] = 9;
				board [0, 6] = 10;
				break;
			}
			break;
		case 15://5*5
			switch (numberPlayers) {
			case 2:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				break;
			case 3:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				board [14, 7] = 9;
				break;
			case 4:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				board [14, 7] = 9;
				board [0, 7] = 10;
				break;
			}
			break;
		case 17://6*6
			switch (numberPlayers) {
			case 2:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				break;
			case 3:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				board [16, 8] = 9;
				break;
			case 4:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				board [16, 8] = 9;
				board [0, 8] = 10;
				break;
			}
			break;
		case 19://7*7
			switch (numberPlayers) {
			case 2:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				break;
			case 3:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				board [18, 9] = 9;
				break;
			case 4:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				board [18, 9] = 9;
				board [0, 9] = 10;
				break;
			}
			break;
		}
		GenerateMap();
		Debug.Log ("It Called the GenerateMap");
	}

public void GenerateMap(){
	int cnt = 0;
	for (int i = 0; i<board.GetLength(0); i++) {
		for (int j = 0; j<board.GetLength(1); j++) {
				int type = board[i,j];
				GameObject.Instantiate(visuals[type],new Vector3(i,-j,0),Quaternion.identity);
			}
		}
	}
}

Code that doesn’t work

using UnityEngine;
using System.Collections;

public class MapMaker {
	public GameObject[] visuals;
	public int[,] board;
	
	void Start(){
		visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
		visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
	}

	public void SetBoard(int size, int numberPlayers){
		board = new int[size, size];
		switch (size) {
		case 11://3*3
			switch (numberPlayers) {
			case 2:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				break;
			case 3:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				board [10, 5] = 9;
				break;
			case 4:
				board [5, 5] = 6;
				board [5, 10] = 7;
				board [5, 0] = 8;
				board [10, 5] = 9;
				board [0, 5] = 10;
				break;
			}
			break;
		case 13://4*4
			switch (numberPlayers) {
			case 2:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				break;
			case 3:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				board [12, 6] = 9;
				break;
			case 4:
				board [6, 6] = 6;
				board [6, 12] = 7;
				board [6, 0] = 8;
				board [12, 6] = 9;
				board [0, 6] = 10;
				break;
			}
			break;
		case 15://5*5
			switch (numberPlayers) {
			case 2:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				break;
			case 3:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				board [14, 7] = 9;
				break;
			case 4:
				board [7, 7] = 6;
				board [7, 14] = 7;
				board [7, 0] = 8;
				board [14, 7] = 9;
				board [0, 7] = 10;
				break;
			}
			break;
		case 17://6*6
			switch (numberPlayers) {
			case 2:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				break;
			case 3:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				board [16, 8] = 9;
				break;
			case 4:
				board [8, 8] = 6;
				board [8, 16] = 7;
				board [8, 0] = 8;
				board [16, 8] = 9;
				board [0, 8] = 10;
				break;
			}
			break;
		case 19://7*7
			switch (numberPlayers) {
			case 2:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				break;
			case 3:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				board [18, 9] = 9;
				break;
			case 4:
				board [9, 9] = 6;
				board [9, 18] = 7;
				board [9, 0] = 8;
				board [18, 9] = 9;
				board [0, 9] = 10;
				break;
			}
			break;
		}
		GenerateMap();
		Debug.Log ("It Called the GenerateMap");
	}

public void GenerateMap(){
	int cnt = 0;
	for (int i = 0; i<board.GetLength(0); i++) {
		for (int j = 0; j<board.GetLength(1); j++) {
				int type = board[i,j];
				GameObject.Instantiate(visuals[type],new Vector3(i,-j,0),Quaternion.identity);
			}
		}
	}
}

Class used to call the code that doesnt work:

using UnityEngine;
using System.Collections;

public class MapTest : MonoBehaviour {

	void Start(){
		MapMaker map = new MapMaker ();
		map.SetBoard (13, 4);
	}
}

MapMaker is not a MonoBehaviour, its Start method won’t be called on its own. Since Start is not executed, visuals has not been initialized by the time you try to generate the map.

Replace the Start method by a constructor, and that should do the trick.

 public class MapMaker {
     public GameObject[] visuals;
     public int[,] board;
     
     public MapMaker() {
         visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
         visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
     }

...
}