variables based on array.length not updating in JS

So I have a grid based puzzle game that uses a 2D array and some for loops to set up the grid. In the for loops, I have it set so that gridH and gridW (the height and width of the grids) are set to array.length and array[0].length. That way I figure if the array is 4 wide and 4 tall, it will pull those sizes and then be used in the for loops since different levels will have different grid sizes. However, having gridH set to array.length isn’t working and the value in the inspector says 0 and the for loops don’t work correctly. I have to set the in the inspector to 4 (for the 4x4 example) and then it works fine.

Now i could just manually set gridH and gridW for each level, but it would obviously be much easier if it could just work based on the array length. Am I just setting them wrong? Here is what the code for that area looks like. I apologize if this is an easy fix but I can’t figure out why it’s not working.

//This will create a 2D array that will hold some spots and data for various squares
//9 represents a blank square where a player can place an orb
//0 represent a square filled with a blank orb
var startX : int = -1; // this is the left hand side of the puzzle grid
var startY : int = 1; // this is the top of the puzzle grid
var gridArray = [
[9, 0, 9, 0],
[9, 9, 0, 9],
[9, 9, 9, 9],
[0, 0, 9, 0]];
 
///This will end up placing the correct objects in the level based on gridArray
static var r : int;
static var c : int;
var gridW : int = gridArray[0].length; //gets the width of the gridArray for the for loops
var gridH : int = gridArray.length; //gets the height of the gridArray for the for loops
for(r = 0; r < gridW; r++)
{
    for(c = 0; c < gridH; c++)
    {
                var currentX = startX + (r*.96); // this moves newsprites right for each new column being created
                var currentY = startY - (c*.96); // this moves new sprites down for each new row being created
                // 1 pixel seems to be .01 unity units. 100 pixels would be 1 unit
         
                // r and c are reversed in the if statements so the squares are placed left to right
                if(gridArray
[r] == 9)
            {
                    Instantiate(obj_BlankSpot, Vector3 (currentX, currentY, 0), Quaternion.identity);  //this object is a blank square where a player can place an or
            }
       
            else if(gridArray[c][r] == 0)
            {
                    Instantiate(obj_PlacedBlankOrb, Vector3 (currentX, currentY, 0), Quaternion.identity); // this object is a square that hold a blank orb that can be broken
            }
            }
    }

I assume this code is outside of any function. Assignments outside of a function to public class variables (like gridW and gridH) are only done at the time the script is attached to the game object. After that, changes can only be made in the inspector. To fix your problem, move your calculations into the Start() function. In addition, if you are calculating values in Start(), make the variables private.

private var gridW; //gets the width of the gridArray for the for loops
private var gridH; //gets the height of the gridArray for the for loops

function Start() {
    gridW = gridArray[0].length; //gets the width of the gridArray for the for loops
    gridH = gridArray.length; //gets the height of the gridArray for the for loops
}