Null reference exception
I'm working on this project from a book and following it word for word and I'm still having issues. In my layout script I keep getting an error saying that I have a null reference exception at line at line 135 or ti.transform.parent = tileAnchor; can someone help me out and tell me what I'm missing. I thought I referenced it with:
public Transform tileAnchor;
I apologize if this is easy to fix, I've only used Unity sparingly for a couple of months now and I'm trying to understand how it works.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class TileTex {
// This class enables us to define various textures for tiles
public string str;
public Texture2D tex;
}
public class LayoutTiles : MonoBehaviour {
static public LayoutTiles S;
public TextAsset roomsText; // The Rooms.xml file
public string roomNumber = "0"; // Current room # as a string
// ^roomNumber as string allows encoding in the XML & rooms 0-F
public GameObject tilePrefab; // Prefab for all Tiles
public TileTex[] tileTextures; // A list of named textures for Tiles
public bool ___________;
public PT_XMLReader roomsXMLR;
public PT_XMLHashList roomsXML;
public Tile[,] tiles;
public Transform tileAnchor;
void Start() {
S = this; // Set the Singleton for LayoutTiles
// Make a new GameObject to be the TileAnchor (the parent transform of
// all Tiles). This keep Tiles tidy in the Hierarchy pane.
GameObject tAnc = new GameObject("TileAnchor");
tileAnchor = tAnc.transform;
// Read the XML
roomsXMLR = new PT_XMLReader(); // Create a PT_XMLReader
roomsXMLR.Parse(roomsText.text); // Parse the Rooms.xml file
roomsXML = roomsXMLR.xml["xml"][0]["room"]; // Pull all the <room>s
// Build the 0th Room
BuildRoom(roomNumber);
}
//This is the GetTileTex() method that Tile uses
public Texture2D GetTileTex(string tStr) {
// Search though all the tileTextures for the proper string
foreach (TileTex tTex in tileTextures) {
if (tTex.str == tStr) {
return (tTex.tex);
}
}
// Return null if nothing was found
return(null);
}
// Build a room based on room number. This is an alternative version of
// BuildRoom that grabs roomXML based on num.
public void BuildRoom (string rNumStr) {
PT_XMLHashtable roomHT = null;
for (int i=0; i<roomsXML.Count; i++) {
PT_XMLHashtable ht = roomsXML[i];
if (ht.att("num") == rNumStr) {
roomHT = ht;
break;
}
}
if (roomHT == null) {
Utils.tr("ERROR","LayoutTiles.BuildRoom()","Room not found: "+rNumStr);
return;
}
BuildRoom(roomHT);
}
// Build a room from a XML <room> entry
public void BuildRoom (PT_XMLHashtable room) {
// Get the texture names for the floors and walls from <room> attributes
string floorTexStr = room.att("floor");
string wallTexStr = room.att ("wall");
// Split the room into rows of tiles based on carriage returns in the
// Rooms.xml file
string[] roomRows = room.text.Split('\n');
// Trim tabs from the beginnings of lines. However, we're leaving spaces
// and underscores to allow for non-rectangular rooms
for (int i=0; i<roomRows.Length; i++) {
roomRows[i] = roomRows[i].Trim('\t');
}
// Clear the tiles away
tiles = new Tile[ 100, 100 ]; // Arbitrary max room size is 100x100
// Declare a number of local fields that we will use later
Tile ti;
string type, rawType, tileTexStr;
GameObject go;
int height;
float maxY = roomRows.Length - 1;
// These loops scan through each tile of each row of the room
for (int y = 0; y < roomRows.Length; y++) {
for (int x = 0; x < roomRows [y].Length; x++) {
// Set defaults
height = 0;
tileTexStr = floorTexStr;
// Get the character representing the tile
type = rawType = roomRows [y] [x].ToString ();
switch (rawType) {
case " ": //empty space
case "_": // empty space
// Just skip over empty space
continue;
// Skips to the next iteration of the x loop
case ".": // default floor
// Keep type="."
break;
case "|": //defualt wall
height = 1;
break;
default:
// Anything else will be interpreted as floor
type = ".";
break;
}
// Set the texture for floor or wall based on <room> attributes
if (type == ".") {
tileTexStr = floorTexStr;
} else if (type == "|") {
tileTexStr = wallTexStr;
}
// Instantiate a new TilePrefab
go = Instantiate (tilePrefab) as GameObject;
ti = go.GetComponent<Tile>();
// Set the parent transform to tileAnchor
ti.transform.parent = tileAnchor;
// Set the position of the tile
ti.pos = new Vector3 (x, maxY - y, 0);
tiles [x, y] = ti; //Add ti to the tiles 2D Array
// Set the type, height and texture of the Tile
ti.type = type;
ti.height = height;
ti.tex = tileTexStr;
// More to come here...
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612101515im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How select between diferent variables from a referenced script with one variable 2 Answers
How to Access the Collider Just Triggered. 0 Answers
How can I reference "score" in one script and then use it in another? 1 Answer
After Reloading scene, reference is switched to another refrence 1 Answer
NullReferenceException: Object reference not set to an instance of an Object 1 Answer