Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Optimus_L · Jul 15, 2015 at 12:48 PM · array2d array

Setting a property in a 2D array giving me an Object not set to Instance error.

I've created a class called Cell which has one property which is an int called Value. I'm trying to create a 2D array to create a grid of cells with random values, however whenever I try to set the value of Cell [x, y] I get this error;

NullReferenceException: Object reference not set to an instance of an object

I know this means that the object isn't getting created correctly, but I can't see why.

 My Cell class
 public class Cell : iCell {
 
     int _value;
 
     public int Value {
         get {return _value;}
         set {_value = value; }
 }
     public Cell ()
         {
             _value = 0;
         }
 }

My Grid Class

 public class SudokuGrid : iSudokuGrid {
 
         private Cell[,] _cells;
         private Cell _currentCell;
         private Cell _previousCell;
 
 
         public Cell[,] Cells {
             get { return _cells; }
             set { _cells = value; }
         }    
         public Cell CurrentCell {
             get { return _currentCell;}
             set { _currentCell = value;}
         }
         public Cell PreviousCell {
             get { return _previousCell;}
             set { _previousCell = value;}
         }
 
         public SudokuGrid() {
 
             Cell [,] _cells = new Cell[9,9];
             //int x = 0;
             //int y = 0;
         }
 
         public void Generate() {
 
             for (int x = 0; x < 9; x++) {
                 for (int y = 0; y < 9; y++) {
                     int rnd = Random.Range(1,9);
                     _cells[x, y].Value = rnd;
                     UnityEngine.Debug.Log(x.ToString() + " " + y.ToString());
 
                 }
             }    
             UnityEngine.Debug.Log("Process finsihed ");
         }
 }


The error is coming from line 38

 _cells[x, y].Value = rnd;

I've created a script called Debug and I generate the Grid object in start(), not I've switched up the code a bit while trying to figure out what was going wrong...

 void Start () {
         SudokuGrid grid = new SudokuGrid ();    
         //grid.Generate ();
         grid.Cells [2, 3].Value = 5;
         UnityEngine.Debug.Log (grid.Cells [2, 3].Value.ToString ());
 }
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
0

Answer by _dns_ · Jul 15, 2015 at 02:33 PM

Hi, you instantiate an array of Cells. This is correct but the elements of the array are Cell reference or pointer, but they are each empty = null after the array is instantiated.

You need to instantiate a new Cell() for each element of the array!

If it was a value type like an int or a struct you wouldn't need to do that, but as it is a class, they need to be instantiated one by one.

Comment
Add comment · Show 4 · 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 Optimus_L · Jul 16, 2015 at 08:39 AM 0
Share

Thanks for the reply.

However I tried this and I'm still getting the same error. I've tried adding this to both the constructor and the Generate() method and neither worked.

  for (int x = 0; x < 9; x++) {
                  for (int y = 0; y < 9; y++) {
                       _cells[x, y] = new Cell();
    }
 }


avatar image _dns_ · Jul 16, 2015 at 11:39 AM 0
Share

The SudokuGrid constructor should be the place to do it, and your code seems correct.

On the line "public class Cell : iCell", what is this iCell it derives from ?

avatar image Optimus_L · Jul 16, 2015 at 01:15 PM 0
Share

iCell is the interface for the Cell class.

I'll have another crack at it when I get home from work.

avatar image _dns_ · Jul 16, 2015 at 01:32 PM 0
Share

Ho, on line 23 "Cell [,] _cells = new Cell[9,9];": a new _cell[,] is created that is not the private member _cell and is deleted at the constructor's exit... That's why _cell itself is null later in Generate(). I guess that's your first bug. Not instantiating a cell for each element of the array would have cause further errors as well :)

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

22 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

Related Questions

2D array generated with custom inspector, null when starting game? 1 Answer

Mouse Position to 2D array 1 Answer

What is a 2D Array? 3 Answers

2D Array of GameObjects... 1 Answer

Save a precalculated grid path into a jagged 2d array ready to be loaded when need 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