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 $$anonymous$$ · Dec 14, 2014 at 08:09 PM · javascriptmonodevelopdebuggingarray-out-of-range-except

3D array in Unity script (javascript) + debuging

I just started using Unity and I've run into little bit of hurdle. I wanted to make 3D array to store state of cellular automaton, a task which was trivial in C++, unfortunately despite my best efforts the script throws IndexOutOfRangeException: Array index is out of range. (wrapper managed-to-managed) object:ElementAddr (object,int,int,int) array.Start () (at Assets/array.js:6) error. While I understand what error means I don't understand while following code

 public var mat:int[,,] = new int[100,100,100];
 function Start () {
 for(var x : int = 0; x < mat.Length; x++){
 for(var z : int = 0; z < mat.Length; z++){
 for(var y : int = 0; y < mat.Length; y++){
 mat[x,z,y] = binRandom();
 }
 }
 }
 }
 function binRandom(){
 var n;
 var x = Random.Range(0.0, 1.0);
 if (x < 0.5){
 n = 1;
 }
 if (x > 0.5){
 n = 0;
 }
 return n;
 }

produces it.

Somewhat related to that is trouble I've been having with Monodevelop debugger.

When I attach it to unity editor and then run the game the entire application freezes and process has to be terminated. I have been following instruction from Unity manual so I don't know where the problem is.

Thank you for replies.

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 Eric5h5 · Dec 14, 2014 at 08:21 PM 1
Share

You can replace your binRandom function with

 mat[x,z,y] = Random.Range (0, 2);

which will pick either 0 or 1. Also, never do stuff like:

 var n;

Always specify the type, or supply a value so the type can be inferred. Also it's better if you stick to the standard Unity convention of using uppercase for function names and lowercase for variables; makes code easier to understand.

1 Reply

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

Answer by zharik86 · Dec 14, 2014 at 08:13 PM

Multi-demintion array matter Length as work of all internal arrays. In your case of Length = 100*100*100. And of course you receive a mistake as, for example, the element mat[100*100, 100*100, 100*100] doesn't exist. Change a code a little:

  public var mat:int[,,] = new int[100,100,100];
  
  function Start () {
   //Sequentially for array x, z, y
   for(var x : int = 0; x <= mat.GetUpperBound(0); x++) {
    for(var z : int = 0; z <= mat.GetUpperBound(1); z++) {
     for(var y : int = 0; y <= mat.GetUpperBound(2); y++) {
      mat[x,z,y] = binRandom();
     }
    }
   }
  }

I hope that it will help you.

Comment
Add comment · Show 6 · 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 Eric5h5 · Dec 14, 2014 at 08:19 PM 1
Share

I'd recommend GetLength rather than GetUpperBound, since you use it the same way you use Array.Length. Your code has a bug at one place where you didn't change < to <=, but if you use GetLength, you don't have to change that anyway.

avatar image zharik86 · Dec 14, 2014 at 09:31 PM 0
Share

@Erich5h5 Thanks, it's my mistake:) But in fact, in what a difference between GetUpperBound and GetLength. Execution time?

avatar image $$anonymous$$ · Dec 15, 2014 at 04:19 PM 0
Share

Sorry but I can't find GetUpperBound in unity documentation any idea where to find more about it?

avatar image Eric5h5 · Dec 15, 2014 at 04:45 PM 0
Share

@$$anonymous$$: GetUpperBound etc. aren't in the Unity docs since they're already thoroughly documented on $$anonymous$$SDN.

@zharik86: As I mentioned, GetLength is like Array.Length, where it's the total length. GetUpperBound is the upper bound (length - 1), which is usable obviously but can trip you up when you're used to Array.Length and List.Count; no sense inviting bugs.

avatar image zharik86 · Dec 15, 2014 at 07:33 PM 0
Share

@Erich5h5 I use both methods in the programs and didn't make mistakes yet:) In documentation of $$anonymous$$SDN give the example for GetUpperBound method for multi-demention arrays. Perhaps, at the initial stage of program$$anonymous$$g can confused. But it is necessary to learn various combinations of one task at once.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Writing to Monodevelop's "Application Output" window 3 Answers

Trivial question for Javascript/Unityscript experts: static variables? 4 Answers

My compiler Monodevelop doesn't build. 1 Answer

Why does monodevelop sets "pink", nonfunctioning breakpoints ? 1 Answer

JSript Error monoDevelop (built in) 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