noobtuts tetris tutorial array index of of range exception

Hi all,

I am working on noobtuts 2d tetris tutorial and at the end i see it has 16 cube tetris height and i change the spawner position from (5,14) to (5,18) and i changed main camera’s position (4.5, 7,5) to (4.5, 9.5) and size from 8 to 10 . it works but gives me “index out of range exception: Array index is out of range” on line 136 Group.cs. script is below as red line gives exception and there is one more script as Grid.cs. it is too below.

Where did i make wrong? Thanks for your help.

public class Group : MonoBehaviour {

// Use this for initialization
void Start() {
// Default position not valid? Then it’s game over
if (!isValidGridPos()) {
Debug.Log(“GAME OVER”);
Destroy(gameObject);
}
}

// Time since last gravity tick
float lastFall = 0;

// Update is called once per frame
void Update() {

// Move Left
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
// Modify position
transform.position += new Vector3(-1, 0, 0);

// See if valid
if (isValidGridPos())
// It’s valid. Update grid.
updateGrid();
else
// It’s not valid. revert.
transform.position += new Vector3(1, 0, 0);
}

// Move Right
else if (Input.GetKeyDown(KeyCode.RightArrow)) {
// Modify position
transform.position += new Vector3(1, 0, 0);

// See if valid
if (isValidGridPos())
// It’s valid. Update grid.
updateGrid();
else
// It’s not valid. revert.
transform.position += new Vector3(-1, 0, 0);
}

// Rotate
else if (Input.GetKeyDown(KeyCode.UpArrow)) {
transform.Rotate(0, 0, -90);

// See if valid
if (isValidGridPos())
// It’s valid. Update grid.
updateGrid();
else
// It’s not valid. revert.
transform.Rotate(0, 0, 90);
}

// Fall
else if (Input.GetKeyDown(KeyCode.DownArrow)) {
// Modify position
transform.position += new Vector3(0, -1, 0);

// See if valid
if (isValidGridPos()) {
// It’s valid. Update grid.
updateGrid();
} else {
// It’s not valid. revert.
transform.position += new Vector3(0, 1, 0);

// Clear filled horizontal lines
Grid.deleteFullRows();

// Spawn next Group
FindObjectOfType().spawnNext();

// Disable script
enabled = false;
}
}

// Move Downwards and Fall
else if (Input.GetKeyDown(KeyCode.DownArrow) ||
Time.time - lastFall >= 1) {
// Modify position

transform.position += new Vector3(0, -1, 0);

// See if valid
if (isValidGridPos()) {
// It’s valid. Update grid.
updateGrid();
} else {
// It’s not valid. revert.
transform.position += new Vector3(0, 1, 0);

// Clear filled horizontal lines
Grid.deleteFullRows();

// Spawn next Group
FindObjectOfType().spawnNext();

// Disable script
enabled = false;
}

lastFall = Time.time;
}
}

bool isValidGridPos() {
foreach (Transform child in transform) {
Vector2 v = Grid.roundVec2(child.position);

// Not inside Border?
if (!Grid.insideBorder(v))
return false;

// Block in grid cell (and not part of same group)?
if (Grid.grid[(int)v.x, (int)v.y] != null &&
Grid.grid[(int)v.x, (int)v.y].parent != transform)
return false;
}
return true;
}

void updateGrid() {
// Remove old children from grid
for (int y = 0; y < Grid.h; ++y)
for (int x = 0; x < Grid.w; ++x)
if (Grid.grid[x, y] != null)
if (Grid.grid[x, y].parent == transform)
Grid.grid[x, y] = null;

// Add new children to grid
foreach (Transform child in transform) {
Vector2 v = Grid.roundVec2(child.position);
Grid.grid[(int)v.x, (int)v.y] = child;
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grid : MonoBehaviour {
// The Grid itself
public static int w = 10;
public static int h = 20;
public static Transform[,] grid = new Transform[w, h];

public static Vector2 roundVec2(Vector2 v) {
return new Vector2(Mathf.Round(v.x),
Mathf.Round(v.y));
}

public static bool insideBorder(Vector2 pos) {
return ((int)pos.x >= 0 &&
(int)pos.x < w &&
(int)pos.y >= 0);
}

public static void deleteRow(int y) {
for (int x = 0; x < w; ++x) {
Destroy(grid[x, y].gameObject);
grid[x, y] = null;
}
}

public static void decreaseRow(int y) {
for (int x = 0; x < w; ++x) {
if (grid[x, y] != null) {
// Move one towards bottom
grid[x, y-1] = grid[x, y];
grid[x, y] = null;

// Update Block position
grid[x, y-1].position += new Vector3(0, -1, 0);
}
}
}

public static void decreaseRowsAbove(int y) {
for (int i = y; i < h; ++i)
decreaseRow(i);
}

public static bool isRowFull(int y) {
for (int x = 0; x < w; ++x)
if (grid[x, y] == null)
return false;
return true;
}

public static void deleteFullRows() {
for (int y = 0; y < h; ++y) {
if (isRowFull(y)) {
deleteRow(y);
decreaseRowsAbove(y+1);
–y;
}
}
}

}

Hello, your grid array size is not big enough

An array always has a fixed size
Say your array is something like this
Grid[5,5] that means it can only go from 0 to 5 on each dimension.
Either you increase your array size or decrease your x and y in this case to fit the array. Goodluck with your progress.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grid : MonoBehaviour {
// The Grid itself
public static int w = 10;
public static int h = 20;
public static Transform[,] grid = new Transform[w, h];

My grid array grid [10,20] and that is exactly fit my new dimensions i think. Where must i change. I can’t understand.

Right above the if line that gives you an error
Add a:
Debug.Log("x: " + (int)v.x + " | y: " + (int)v.y);

This way you can see if you are going outside the dimensions.

Yes i did it, and i see that even though the cubes has reached the height:20 and it must be game over, it continues to spawn the blocks. and then it starts to give “array index out of range exception”. What can i do for that.
Thanks.