# InvalidCastException

Hello! I'm having trouble translating the C# from the Cellular Automata tutorial into Javascript. I've built a constructor inside my function that creates a nested array variable called *matrix*. Then I have a series of *for* loops that first assign each cell in the nested array a number value (1 or 0) and then draw a cube in a location in the game world based on the corresponding cell's value and location in the nested array.

Unfortunately, Unity seems to be having issues with the casting of my nested array variable.

Here's my code. // Declaring/typing variables var w: int; var h: int; var x: int; var y: int; var fill: float;

```
function Update() {
if (Input.GetMouseButtonDown(0)) {
GenerateMap(w, h);
};
};
function GenerateMap(width, height) {
var matrix = new array(width);
for (x = 0; x < width; x++) {
matrix[x] = new array(height);
};
for (x = 1; x < width - 1 ; x++) {
for (y = 1; y < height - 1; y++) {
if (x == 1 || x == width - 1 || y == 1 || y == height - 1) {
matrix[x][y] = 1;
};
else if (Random.value < fill.value) {
matrix[x][y] = 0;
};
else {
matrix[x][y] = 1;
};
};
};
for (x = 1; x < width - 1; x++) {
for (y = 1; y < height - 1; y++) {
if (matrix[x][y] == 1) {
Gizmos.color = Color.black;
};
else {
Gizmos.color = Color.white;
};
var pos = new Vector3();
Gizmos.DrawCube(Vector3.one, Vector3.one);
};
};
};
```

The error comes at line 15, where Unity gets the *matrix* variable. The exact error is

InvalidCastException: Cannot cast from source type to destination type. CaveGenerator.GenerateMap (System.Object width, System.Object height) (at Assets/Scripts/CaveGenerator.js:15) CaveGenerator.Update () (at Assets/Scripts/CaveGenerator.js:10)

I would really appreciate any insight into the problem here.

Are 8 and 10 right? I don't know UnityScript but the matrix 2-dim array definition there (twice?) looks odd.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.