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 Paikerchu13 · Jan 30, 2014 at 05:38 PM · instantiatearraynullreferenceexception

How to Instantiate an Integer Array in an Array of Arrays?

I tried to create an array of arrays in Unity in order to edit the values in the Inspector, as you can't simply edit a multidimensional array. I want to be able to input an X and Y value that will allow me to adjust the length and width of the arrays.

 [System.Serializable]
 [ExecuteInEditMode()]
 public class Row{
     public int[] column;
 }
 
 [System.Serializable]
 [ExecuteInEditMode()]
 public class Array2D{
     public Row[] rows;
 }
 
 [System.Serializable]
 [ExecuteInEditMode()]
 public class test : MonoBehaviour {
     
     public int x;
     public int y;
     
     public Array2D Tiles;
     public bool refresh = false;
     
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
             if(refresh==true){
             setRowCol(x,y);
         }
     }
     
     void setRowCol(int r,int h){
         
         if(r>0&&h>0){
         Tiles.rows = new Row[r];
         
             for(int i=0; i<r;i++){
             Tiles.rows[i].column = new int[h];
             }
         }
         if(r==0||h==0){
             Debug.Log("0 is not a valid value for X or Y");
         }
         refresh=false;
     }
 }

The code works fine for the most part; however, this one line:

             Tiles.rows[i].column = new int[h];

Does not work however much I try. Can someone please tell me what the issue is? The error I get is a NullReferenceException.

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 KellyThomas · Jan 30, 2014 at 05:50 PM

 Tiles.rows = new Row[r];

That line is assigning an array or Rows but not actually instantiating any instances of Row. At that point the array holds a set of null values. You then receive an error when you try to access a property of the null in Tiles.rows[0].

Instead you should assign a new Row instance for each position in the rows[] array, then you can access their properties:

          for(int i=0; i<r;i++){
              Tiles.rows[i] =  new Row();
              Tiles.rows[i].column = new int[h];
          }
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 Paikerchu13 · Jan 30, 2014 at 06:30 PM 0
Share

Thank you, I can't believe I missed something as simple as that!

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

20 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

Related Questions

Material[] Object reference not set when instantiating 2 Answers

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

Make object move in a direction depending on where it spawns? (C#) 1 Answer

Prefabs instantiated from an array are keeping their public int value 1 Answer

Simultaneous Null Reference Exception and expected value 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