• 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 ndrummer31 · Apr 14, 2015 at 10:15 PM · null reference exception2d array

NullReferenceException with 2d Array

Hello, I am trying to make a minesweeper game and need help with finding adjacent cells. I'm getting this error: "NullReferenceException: Object reference not set to an instance of an object" And cannot, for the life of me, find the source of the problem!

Board Class:

 public Tile[,] tiles;
 public Transform boardTransform;
 [SerializeField] private Vector2 boardSize;
 public int bombCount;
 public GameObject tileAsset;

 public static int boardSizeX;
 public static int boardSizeY;

 public static Board board;

 private void Awake ()
 {
     board = this;
 }

 private void Start ()
 {
     boardSizeX = Mathf.FloorToInt ( boardSize.x );
     boardSizeY = Mathf.FloorToInt ( boardSize.y );

     tiles = new Tile[boardSizeX, boardSizeY];
     CreateBoard ();
 }

 private void CreateBoard ()
 {
     if (boardTransform != null)
     {
         for (int x = 0; x < boardSizeX; x++)
         {
             for (int y = 0; y < boardSizeY; y++)
             {
                 Tile newTile = Instantiate ( tileAsset ).GetComponent<Tile> ();

                 newTile.transform.SetParent ( boardTransform );

                 tiles[x, y] = newTile;

                 newTile.xPos = x;
                 newTile.yPos = y;
             }
         }
         foreach (Tile tile in tiles)
         {
             tile.SetAdjacentTiles ();
         }
         PlaceBombs ();
     }
 }

Tile Class:

 public bool isOpen = false;
 public bool isBomb = false;
 public int xPos;
 public int yPos;

 private List<Tile> adjacentTiles;
 private Button _button;
 private Text _text;

 public int adjacentBombCount
 {
     get
     {
         int amount = 0;
         foreach (Tile tile in adjacentTiles)
         {
             if (tile.isBomb)
                 amount++;
         }
         return amount;
     }
 }

 private void Start ()
 {
     _button = GetComponent<Button> ();
     _text = GetComponentInChildren<Text> ();
     adjacentTiles = new List<Tile> ();
 }

 public void SetAdjacentTiles ()
 {
     for (int x = xPos - 1; x <= xPos + 1; x++)
     {
         if (x < 0 || x >= Board.boardSizeX)
             continue;

         for (int y = yPos - 1; y <= yPos + 1; y++)
         {
             if (y < 0 || y >= Board.boardSizeY)
                 continue;

             if (Board.board.tiles[x, y] != this)
                 adjacentTiles.Add ( Board.board.tiles[x, y] );        
         }
     }
 }

The error happens in "SetAdjacentTiles" at "adjacentTiles.Add ( Board.board.tiles[x, y] );"

Please help!

I'm a novice programmer so any advice would be much appreciated!

Thank you :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

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

2D array Null reference 1 Answer

Hex map movement problem 2 Answers

NullReferenceException with MultiDimensional Array 0 Answers

Changing One Value in an Array Changes All Values 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges