Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by MomijiStudios · Apr 09, 2014 at 09:30 AM · gameobjectarraynullreferenceexceptiontable

Making 'table' (2D built-in array) of GameObjects giving "NullReferenceException:" error

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: 1) Tile code (type of tile indicated by a number) 2) 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('\n');
         // 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[i] = 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][i], 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.

Comment
Add comment · Show 1
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
avatar image MomijiStudios · Apr 09, 2014 at 09:29 AM 0
Share

I already know a lot of people say List solves most problems, but I'm stubborn and want to understand why this doesn't work. Also, I DO know how long the array is going to be, and it won't be changing, so I think that means the more efficient built-in array should work just fine.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MomijiStudios · Apr 09, 2014 at 10:08 AM

Of course I fixed it myself just a few minutes after posting it (even though I've been working on it for over 5 hours). I simply made the array as a GameObject[,] instead of GameObject[][].

Honestly I still have no idea why it didn't work that way, but I suppose I'll just deal with not knowing since it works now.

Comment
Add comment · Share
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

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

21 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

Related Questions

Resources.LoadAll(); GameObject[] Array 1 Answer

NullReferenceException error in an array of objects 0 Answers

Save Multiple GameObject with XML 1 Answer

Don't see the cause of this NullReferenceException... 1 Answer

HELP Find gameObject With tag in another array 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