I’m making a level generator. There are two ‘tables’ made with 2D built-in arrays for the tiles that need to be referenced throughout gameplay:
- Tile code (type of tile indicated by a number)
- The actual tile GameObject (so I can delete them when necessary by referencing their position on the table)
Here is the code:
using UnityEngine;
using System.Collections;
public class LevelGenerator : MonoBehaviour {
// NOTES:
// dataPos.Length is the amount of lines.
// data actually starts on dataPos[12][0]
// lines - 2 because there are 2 empty lines after the data
// TILE CODES:
// 0: nothing
// 1: blank white tile
// 2: red paint
// 3: blue paint
// 4: yellow paint
// 5: purple paint
// 6: orange paint
// 7: green paint
// 11: red empty
// 12: blue empty
// 13: yellow empty
// 14: purple empty
// 15: orange empty
// 16: green empty
// 19: 1
// 20: 2
// 21: 3
// 22: 4
// 23: 5
// 24: 6
// 25: 7
// 26: 8
// 27: 9
// 28: start
// 29: exit
// 38: red door
// 39: blue door
// 40: yellow door
// 41: purple door
// 42: orange door
// 43: green door
private string[] dataLines;
private string[][] tileTable;
private int tileRows;
private GameObject[][] tileObjectTable;
public Cube cubeScript;
public float horizontalSpace;
public float verticalSpace;
public GameObject tBlank;
public GameObject tStart;
public GameObject tExit;
public GameObject tPaintRed;
public GameObject tEmptyRed;
public GameObject tDoorRed;
public TextAsset L001;
void Awake () {
// make array with each entry being one line of the text file
dataLines = L001.text.Split('
');
// make table array that has a row length equal to only the lines
// of actual data (there are 14 lines of useless data)
tileTable = new string[dataLines.Length - 14];
// foreach (string line in dataLines) {
for (int i = 0; i < tileTable.Length; i++) {
// put each tile number (separated by commas) as an entry in an
// array for each line
tileTable *= dataLines[12 + i].Split (',');*
-
}*
-
tileObjectTable = new GameObject[tileTable.Length][];*
-
StartCoroutine ("CreateLevel");*
-
}*
-
IEnumerator CreateLevel () {*
-
// generate each tile*
-
for (int currentIncrement = 0; currentIncrement < tileTable.Length; currentIncrement++) {*
-
// do for each tile in the current row*
-
for (int i = 0; i < tileTable[currentIncrement].Length; i++) {*
_ CreateTile(tileTable[currentIncrement], currentIncrement, i);_
* transform.position = new Vector3 (transform.position.x + horizontalSpace, transform.position.y, transform.position.z);*
* yield return new WaitForSeconds (0.05f);*
* //return null;*
* }*
* // finished that row*
* currentIncrement += 1;*
* transform.position = new Vector3 (0, transform.position.y, transform.position.z - verticalSpace);*
* //yield return new WaitForSeconds (1);*
* //return null;*
* }*
* // done with all the level creation, start game*
* //return null;*
* //Destroy (gameObject);*
* }*
* void CreateTile (string tileType, int startRow, int startColumn) {*
* GameObject obj;*
* switch (tileType) {*
* case “1”:*
* tileObjectTable[startRow][startColumn] = (GameObject)Instantiate(tBlank, transform.position, Quaternion.identity);*
* break;*
* case “2”:*
* Instantiate (tPaintRed, transform.position, Quaternion.identity);*
* break;*
* case “11”:*
* Instantiate (tEmptyRed, transform.position, Quaternion.identity);*
* break;*
* case “28”: // start tile*
* Instantiate (tStart, transform.position, Quaternion.identity);*
* // call initialization script, giving it the entire table and the*
* // player starting position on the table*
* cubeScript.PlayerInitialize(tileTable, startRow, startColumn, transform.position, horizontalSpace, verticalSpace);*
* break;*
* case “29”:*
* Instantiate (tExit, transform.position, Quaternion.identity);*
* break;*
* case “38”:*
* Instantiate (tDoorRed, transform.position, Quaternion.identity);*
* break;*
* }*
* }*
}
I’ve researched this for over 5 hours, and I’ve followed every ‘solution’ without any results. When it gets to this line on runtime:
tileObjectTable[startRow][startColumn] = (GameObject)Instantiate(tBlank, transform.position, Quaternion.identity);
I get the following error:
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
LevelGenerator.CreateTile (System.String tileType, Int32 startRow, Int32 startColumn) (at Assets/LevelGenerator.cs:119)
LevelGenerator+c__Iterator0.MoveNext () (at Assets/LevelGenerator.cs:95)
Please help, this is driving me crazy.