- Home /
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.
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.
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.
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.
@Erich5h5 Thanks, it's my mistake:) But in fact, in what a difference between GetUpperBound and GetLength. Execution time?
Sorry but I can't find GetUpperBound in unity documentation any idea where to find more about it?
@$$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.
@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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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