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 /
avatar image
0
Question by matp86 · Aug 27, 2018 at 02:28 PM · listinstantiationnull reference exceptionfor loop2d array

2D array not working- Null Reference Exception: A null value was found where an object instance was required.

I am attempting to build a 2D grid of tiles. I'm using nested for loops within a coroutine to instantiate each tile and then add the tile to an array. Tiles are prefabs with an attached Tile class script. The Tile class holds information about each tile including it's position in the grid.

I was previously using a List instead of a 2D array which worked fine at first, until I realised I need to access each Tile using its coordinates.

As far as I can tell the syntax for the array and the instantiation are correct. I've tried a few different type casts to be sure. I've left the previous list in (commented out) to use as a sanity check to make sure references were working.

The LevelGenerator.cs script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelGenerator : MonoBehaviour {    
     // Level grid dimensions
     public int GridSizeX;
     public int GridSizeY;
 
     // The current position in the grid
     public int GridPosX;
     public int GridPosY;
 
     // List of tile pieces
     // public List <Tile> levelGrid;
 
     // 2D array of tile pieces
      public Tile[,] tileArray;
     
     // Critical path values
     public int criticalPathLength = 8;
     public int criticalPathTilesPlaced = 0;
 
     public bool critPathStarted = false;
 
     // This stores the grid position of the last critical path tile 
     public int lastCritX;
     public int lastCritY;    
 
     // Blank tile prefab to be placed
     public Tile blankTile;    
     //=====================================================================================    
     // Use this for initialization
     void Awake ()
     {            
         // Calls the coroutine that buils the level grid
         StartCoroutine(BuildLevelGrid());        
     }
     
     // Update is called once per frame
     void Update ()
     {
          // Instantiate<Tile>(blankTile, new Vector2(GridPosX, GridPosY), transform.rotation);
     }     
 
     // The level grid is populated here. 
     // There is a pause between placement of each tile for debugging purposes.
     IEnumerator BuildLevelGrid ()
     {          
         // Nested for loops build the grid.
         for (GridPosY = 0; GridPosY < GridSizeY; GridPosY++)
         {
             for (GridPosX = 0; GridPosX < GridSizeX; GridPosX++)
             {
                 // Instantiates blank tiles and adds them to the list
                 // levelGrid.Add(Instantiate(blankTile, new Vector2(GridPosX, GridPosY), transform.rotation));             
                 
                 // Populates the array with new tiles
                 tileArray[GridPosX, GridPosY] = Instantiate<Tile>(blankTile, new Vector2(GridPosX, GridPosY), transform.rotation);
 
                 // Wait before placing the next tile.
                 yield return new WaitForSecondsRealtime(0.2f);                                
             }
         }
     }
 }

On play I receive this error:

Null Reference Exception: A null value was found where an object instance was required. LevelGenerator+c_Iterator0.MoveNext () (at Assets/Scripts/LevelGenerator.cs:64)

Oddly, the Tile prefab is definitely set here. It instantiates one tile and then throws the error.

What I gather from this is that there is an issue while iterating through the nested for loops. It might be possible I'm using values that the 2D array can't see (should I separate the iterator values from the GridPos variables maybe?).

To test I moved line 64 to the update function (see the commented code there with this line):

// Populates the array with new tiles tileArray[GridPosX, GridPosY] = Instantiate(blankTile, new Vector2(GridPosX, GridPosY), transform.rotation);

When this line is in the Update function the grid places each tile where it should be, suggesting the iteration in the for loops and the instantiation are both working correctly. This isn't suitable as it just prints tiles forever and I need to be able to control this.

I haven't seen this error before so I'm honestly baffled as to how I should fix it.

TLDR: List instantiates grid of tiles, 2D array doesn't. Error suggests an issue with the for loop iterators.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mahir-Gulzar · Aug 27, 2018 at 02:58 PM

@matp86 You are missing array initialization in awake :)

 void Awake()
     {
         // Calls the coroutine that buils the level grid
         tileArray = new Tile[GridSizeX, GridSizeY];
         StartCoroutine(BuildLevelGrid());
         
     }
Comment
Add comment · Show 1 · 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
avatar image matp86 · Aug 27, 2018 at 03:24 PM 1
Share

Ahh whoops! Thanks that works perfectly :)

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

92 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 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

A node in a childnode? 1 Answer

can somebody help for the algorithm or code how implement this crossword math? 1 Answer

Checking an expression against 2D array elements behaving erratically 0 Answers

Generic list of objects, then distance to those objects? (js) 1 Answer

Card/Tile Game, loops dont update element0 of playerHand properly 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