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
1
Question by robinking · Nov 16, 2010 at 12:47 PM · javascriptmultidimarray

Can I use variables when using the MultiDim script?

I'm using the MultiDim helper script, and it's throwing a problem when I try to define a multidimensional array with sizes set by variables (not explicitly set in the editor - they will be determined by code):

EDIT - here's a script showing just the problem This gets rid of the whole nodes thing in the script I'd included earlier and just shows the MultiDim problem. This script sets random sizes for a 3 dimensional array, and then attempts to fill each entry in the array with the number 1. The console error shows that even accessing Index(0,0,0) proves out of range.

Here it is, all outside of any function. This produces an Array Index Out Of Range error:

var mazeWidth : int; var mazeHeight : int; var mazeDepth : int;

mazeWidth = Random.Range(5,10); // anything from 5 to 9 mazeHeight = Random.Range(5,10); // anything from 5 to 9 mazeDepth = Random.Range(5,10); // anything from 5 to 9

Debug.Log( mazeWidth.ToString() ); Debug.Log( mazeHeight.ToString() ); Debug.Log( mazeDepth.ToString() );

var mazeNode = MultiDim.IntArray(mazeWidth,mazeHeight,mazeDepth);

for (var a =0;a<mazeWidth;a++) { for (var b=0;b<mazeWidth;b++) { for (var c=0;c<mazeWidth;c++) { Debug.Log( "Filling "+a.ToString()+","+b.ToString()+","+c.ToString() ); mazeNode[a,b,c] = 1; } } }

And here it is with functional code inside the Awake function. This results in a "Field 'System.Int32[,,]' not found" error.

var mazeWidth : int; var mazeHeight : int; var mazeDepth : int;

var mazeNode;

function Awake() { mazeWidth = Random.Range(5,10); // anything from 5 to 9 mazeHeight = Random.Range(5,10); // anything from 5 to 9 mazeDepth = Random.Range(5,10); // anything from 5 to 9

 Debug.Log( mazeWidth.ToString() );
 Debug.Log( mazeHeight.ToString() );
 Debug.Log( mazeDepth.ToString() );

 mazeNode = MultiDim.IntArray(mazeWidth,mazeHeight,mazeDepth);

 for (var a =0;a&lt;mazeWidth;a++) {
     for (var b=0;b&lt;mazeWidth;b++) {
         for (var c=0;c&lt;mazeWidth;c++) {
             Debug.Log( "Filling "+a.ToString()+","+b.ToString()+","+c.ToString() );
             mazeNode[a,b,c] = 1;
         }
     }
 }

}

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 Proclyon · Nov 16, 2010 at 02:20 PM 0
Share

I recall a pattern in these kind of mistakes. It is probably misinformation by the error logging. When I get reports which clearly show me something is impossible when it is, start looking for syntax errors. Just segment the problem into chunks with comment blocks. But before that always blame yourself before the computer :P It's usually correct

2 Replies

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

Answer by duck · Nov 16, 2010 at 01:21 PM

Yes you can do this, it's totally valid as written and it should not cause the symptoms you're describing - so I have to guess that your "index out of range" errors are caused by something else.

Are you sure that you're not forgetting that array indices are zero-based? I.E. for a dimension that has a size of "9", the range of valid indices is from 0-8 inclusive (and not 9!).

Perhaps you could paste a larger sample of the code you're using.

--EDIT--

So, looking at the new piece of sample code you pasted in a comment:

xSize = node.transform.position.x;
ySize = node.transform.position.y;
zSize = node.transform.position.z; 
var mazeNode = MultiDim.IntArray(xSize,ySize,zSize); 

In the above code, you're using float values (the object position values) as input to a function which accepts integers, so these are going to get rounded. Perhaps this is your problem.

You really need to trace out the exact values that are being used to track this problem down, so perhaps you should do something like this, and look in the console at which values are really being used for the size and index numbers.

var xSize : int = node.transform.position.x; var ySize : int = node.transform.position.y; var zSize : int = node.transform.position.z; print("defining array of size "+xSize+" x "+ySize+" x "+zSize); var mazeNode = MultiDim.IntArray(xSize,ySize,zSize);

var xPos : int = someOtherPosition.x; var yPos : int = someOtherPosition.y; var zPos : int = someOtherPosition.z; print("inserting value at "+xPos+", "+yPos+", "+zPos); mazeNode[xPos,yPos,zPos] = someValue;

Tracing all this out to the console should help you narrow this down to the cause.

-- EDIT 2!--

Having tried out the sample code you pasted, it seems to run fine. However there are potential problems with it - the main one being the way you determine the array size. If any of your objects have a negative x, y or z value, they will mess up your maze size (since it seems your code assumes they will all have positive values - your code only checks for the max values and not the minimum).

What you should do is capture both the minimum and maximum values, and calculate the maze dimension sizes using the difference between the minimum and maximum. Eg, if your nodes x positions range from -12 to 14, your maze x dimension size would need to be 26 (whereas your current code would set it as 14!).

Hope this helps pinpoint the problem!

-- EDIT 3! --

Okay, this looks like the problem - there are two errors. First:

"Field 'System.Int32[,,]' not found" error.

This is occurring because you're declaring the var outside the function, but you're not specifying a type. This results in the variable becoming a special "dynamically typed" variable, and as such needs to be handled in special ways that I'm not entirely familiar with (because I mainly use C#!). To solve this, you can give the variable a specific type of a 3D int array by assigning a dummy value to it when defining it outside a function, like so:

var mazeNode = MultiDim.IntArray(1,1,1);

You can then assign the real size later. The important thing is that because you gave it a value when assigning it, it has a static type.

Secondly (and you'll kick yourself here), you are using "mazeWidth" as the limit in your for loops for each of the a, b, and c dimensions, instead of using mazeWidth, mazeHeight, mazeDepth! So the fixed code in its entirety would look like this:

var mazeWidth : int; var mazeHeight : int; var mazeDepth : int;

var mazeNode = MultiDim.IntArray(1,1,1);

function Awake() {
mazeWidth = Random.Range(5,10); // anything from 5 to 9
mazeHeight = Random.Range(5,10); // anything from 5 to 9
mazeDepth = Random.Range(5,10); // anything from 5 to 9

 Debug.Log( mazeWidth.ToString() );    
 Debug.Log( mazeHeight.ToString() );    
 Debug.Log( mazeDepth.ToString() );

 mazeNode = MultiDim.IntArray(mazeWidth,mazeHeight,mazeDepth);

 for (var a =0;a&lt;mazeWidth;a++) {    
     for (var b=0;b&lt;mazeHeight;b++) {    
         for (var c=0;c&lt;mazeDepth;c++) {    
             Debug.Log( "Filling "+a.ToString()+","+b.ToString()+","+c.ToString() );    
             mazeNode[a,b,c] = 1;    
         }    
     }    
 }    

}

Comment
Add comment · Show 10 · 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 robinking · Nov 16, 2010 at 07:00 PM 0
Share

As stated in other comment, I actually oversimplified for the purpose of posting - the variables won't be declared as numbers but will be calculated (from the positions of objects within my scene). In a nutshell I have a matrix of 'node' objects, and I have an opening function which calculates the width, depth and height of a bounding box which will contain all the nodes. Then I want to declare the multidim array.

avatar image duck ♦♦ · Nov 17, 2010 at 08:01 AM 0
Share

(edited answer, based on this new information)

avatar image robinking · Nov 18, 2010 at 09:52 AM 0
Share

Unfortunately it's still not working... I tried one other thing, which was to separate the declaration from the assignment. So: var mazeNode; mazeNode = $$anonymous$$ultiDim.IntArray(sizex, sizey, sizez); But it still said array index out of range. I tried: var mazeNode : int; but it gave me the error "Int does not support slicing" I tried: var mazeNode : int[]; but it gave the error "mazeNode is rank 1 not rank 3" I tried: var mazeNode : int[,,]; but it gave a basic syntax error. I'm starting to think I may have to create this entire script in C#...

avatar image duck ♦♦ · Nov 18, 2010 at 10:44 AM 0
Share

maybe you should paste your entire script, rather than snippets of it - otherwise we can only guess at what the problem might be, which is unlikely to be accurate.

avatar image robinking · Nov 19, 2010 at 02:00 AM 0
Share

I've edited the original question to include the entire script - thanks for giving me your time on this, I really appreciate it.

Show more comments
avatar image
2

Answer by Mike 3 · Nov 16, 2010 at 01:22 PM

Make sure than xSize, ySize and zSize in the inspector are the same values as in the script, it could be that they're lower there if you've changed the script values at any point

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 duck ♦♦ · Nov 16, 2010 at 01:30 PM 0
Share

ooh good call :)

avatar image robinking · Nov 16, 2010 at 01:38 PM 0
Share

Perhaps I should clarify further as I was simplifying for the question... The variables are actually deter$$anonymous$$ed by code at Awake(), based on the positions of node objects I will place in each scene - so they are never explicitly set at arbitrary numbers. Sorry for confusing things!

avatar image robinking · Nov 16, 2010 at 01:44 PM 0
Share

Something like this: xSize = node.transform.position.x; ySize = node.transform.position.y; zSize = node.transform.position.z; var mazeNode = $$anonymous$$ultiDim.IntArray(xSize,ySize,zSize);

avatar image duck ♦♦ · Nov 17, 2010 at 08:00 AM 0
Share

I edited my answer to add more information based on your comment above.

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

No one has followed this question yet.

Related Questions

Trouble with MultiDim js arrays 1 Answer

How to declare and initialize multidimensional arrays JS? 4 Answers

JavaScript multidimensional array editing 1 Answer

Initialize multidimensional boolean array as all false Unity JS 2 Answers

IntArray vs BoolArray JS via C# - MultiDim.cs 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