Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tcclark · Nov 27, 2016 at 11:38 AM · referencing

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...
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges