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:
And here it is with functional code inside the Awake function. This results in a "Field 'System.Int32[,,]' not found" error.
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.
So, looking at the new piece of sample code you pasted in a comment:
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.
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:
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:
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:
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
answered Nov 16 '10 at 01:22 PM