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 yargotmo · May 22, 2018 at 08:02 PM · nullreferenceexceptionindexoutofrangeexception

Minesweeper UI Issue

I'm making a minesweeper game and I am using the code below. from two separate classes.

 using System.Collections;
 using UnityEngine;
 
 public class Element : MonoBehaviour
 {
     //bool used to identify whether there is a mine or not hence True/False
     public bool mine; 
 
     public Sprite[] emptyTextures; //array for sprites
     public Sprite mineTexture;
 
     private void Start()
     {//Randomly decide whether it is a mine or is not
         mine = Random.value < 0.15;
 
         //converts from float to int
         int x = (int)transform.position.x;
         int y = (int)transform.position.y;
         NewGrid.elements[x, y] = this;   
 
       
 
 
 
     }
 
     public void loadTextures (int count) //counts the number of surrounding mines 
     {
         //will check if element is a mine or not 
 
         //if yes it loads the texture 
         if(mine) 
             GetComponent<SpriteRenderer>().sprite = mineTexture; 
         //if not it loads a number from the empty textures array
         else
             GetComponent<SpriteRenderer>().sprite = emptyTextures[count];
             
         
     }
 
     public bool isCovered () {  // checks if sprite is covered
 
         return GetComponent<SpriteRenderer>().sprite.texture.name == "Default";
     }
 
     void check (){ //check if player has clicked sprite and check if clicked is mine
 
         //if it is a mine
         if (mine) {
             //uncover all mines 
             NewGrid.uncoverMines();
 
             //notify user that they have lost 
             print("You Lose...LOSER");
         } else {
             // show adjacent mine number
             int x = (int)transform.position.x;
             int y = (int)transform.position.y;
             loadTextures(Grid.adjacentMines(x, y));
             // uncover area without mines
             NewGrid.FFuncover(x, y, new bool[NewGrid.w, NewGrid.h]);
 
             // find out if the game was won now
             if (NewGrid.isFinished())
                 print("you win");
         }
     } 
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewGrid
 {
 
     public static int w = 10; //Width
     public static int h = 13; //Height
 
     //creates 2D element array that takes in the width and height variables
     public static Element[,] elements = new Element[w, h];
 
 
     //function to uncover mines
     public static void uncoverMines()
     {
         foreach (Element elem in elements)
             if (elem.mine)
                 elem.loadTextures(0);
     }
 
     // Find out if a mine is at the coordinates
     public static bool mineAt(int x, int y)
     {
         // If coordinates in range check for mine.
         if (x >= 0 && y >= 0 && x < w && y < h)
             return elements[x, y].mine;
         return false;
     }
 
     // Count adjacent mines for an element
     public static int adjacentMines(int x, int y)
     {
         int count = 0;
 
         if (mineAt(x, y + 1)) ++count; // top
         if (mineAt(x + 1, y + 1)) ++count; // top-right
         if (mineAt(x + 1, y)) ++count; // right
         if (mineAt(x + 1, y - 1)) ++count; // bottom-right
         if (mineAt(x, y - 1)) ++count; // bottom
         if (mineAt(x - 1, y - 1)) ++count; // bottom-left
         if (mineAt(x - 1, y)) ++count; // left
         if (mineAt(x - 1, y + 1)) ++count; // top-left
 
         return count;
     }
 
     // Flood Fill empty elements
     public static void FFuncover(int x, int y, bool[,] visited)
     {
         // Coordinates in Range?
         if (x >= 0 && y >= 0 && x < w && y < h)
         {
             // visited already?
             if (visited[x, y])
                 return;
 
             // uncover element
             elements[x, y].loadTextures(adjacentMines(x, y));
 
             // close to a mine? then no more work needed here
             if (adjacentMines(x, y) > 0)
                 return;
 
             // set visited flag
             visited[x, y] = true;
 
             // recursion
             FFuncover(x - 1, y, visited);
             FFuncover(x + 1, y, visited);
             FFuncover(x, y - 1, visited);
             FFuncover(x, y + 1, visited);
         }
 
     }
 
     public static bool isFinished()
     {
         // Try to find a covered element that is no mine
         foreach (Element elem in elements)
             if (elem.isCovered() && !elem.mine)
                 return false;
         // There are none => all are mines => game won.
         return true;
     }
 }
 
 

My issue is that when I run the build on visual studio code it compiles perfectly but when I attempt to run on game mode I am hit with several errors all saying the same thing. The error is: NullReferenceException: a null value was found where an object instance was required

it keeps pointing to line 19 which has this code NewGrid.elements[x, y] = this;

I didn't initiate the array to a fixed value I just initiated a 2D array so I don't understand why I am getting this error? Error

screen-shot-2018-05-22-at-120211-pm.png (125.9 kB)
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 andrew-lukasik · May 22, 2018 at 08:11 PM 0
Share

Just to be sure, add this assertion to line above the error there:

 UnityEngine.Assertions.Assert.IsNotNull(NewGrid.elements,"NewGrid.elements is definately null");

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

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

NullReferenceException, Script Error 1 Answer

GetComponent returns null 1 Answer

NullReferenceException 0 Answers

Reference lost in NetworkEntity 0 Answers

NullReferenceException while raycasting? 0 Answers


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