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 8bitsdeep · Sep 19, 2012 at 02:49 PM · javascriptarrays2d array

Javascript 2D arrays: null values and slicing

Been banging my ahead against JS 2D arrays for a day now. Every answer I look up seems to just lead to another error in a cyclical mess.

I just want a 2D int array initialized to 0. The below gives me an error: "NullReferenceException: A null value was found where an object instance was required" on houseGrid[x,z] = 0;. I've had slicing errors with various other attempts. I'm at my wits end. Help would be greatly appreciated!

 #pragma strict
 
 var floor : Transform;
 var wall : Transform;
 
 var houseSize : int = 50;
 var minRoomSize : int = 5;
 var maxRoomSize : int = 15;
 
 function Start () {
 
     var x : int = 0;
     var z : int = 0;
     var i : int = 0;    
     
     var houseGrid : int[,];
     houseGrid = new int[houseSize,houseSize];
     
     for(z=0; z<houseSize; z++){
         for(x=0; x<houseSize; x++){
             houseGrid[x,z] = 0;
         }        
     }
     
 } 
Comment
Add comment · Show 2
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 8bitsdeep · Sep 19, 2012 at 02:55 PM 0
Share

That gives a slicing error as well as "Array houseGrid is rank '2', not rank '1'" error for the houseGrid[x,z] = 0 line.

avatar image 8bitsdeep · Sep 19, 2012 at 03:15 PM 0
Share

I tempted to say I've read every relevant answer on here. No dice. :< I'm sure I'm just missing something silly.

1 Reply

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

Answer by Eric5h5 · Sep 19, 2012 at 03:50 PM

The code you posted here compiles and runs without errors. However, as to this:

> I just want a 2D int array initialized to 0.

All you need is:

 function Start () {
     var houseGrid = new int[houseSize, houseSize];
 }

An int can't be null, so an int[,] array always has every entry initialized to 0.

Comment
Add comment · Show 2 · 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 8bitsdeep · Sep 19, 2012 at 04:00 PM 0
Share

Oh for fuck's sake... I think I had it correct at some point but didn't hit play again to clear the error. (Still new to Unity, not used to the clear on play vs on change errors)

Thanks for pointing out the initialization thing!

avatar image Eric5h5 · Sep 19, 2012 at 04:28 PM 0
Share

There's a clear button in the console, so you don't need to hit play to clear errors.

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

11 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

Related Questions

Javascript 2d array Help 0 Answers

Public 2D array, accessed through the editor ?? 3 Answers

Find colliders after collision? 2 Answers

Problem removing items from array 2 Answers

List problem? 2 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