- Home /
Tetris game - Destroy game object with tag doesn't work and Index is out of bounds of array,Tetris game -Destroy game object with tag doesn't destroy and Index is out of bounds of array
I started learning Unity & C# and decided to develop a Tetris game as a practice.
I have most functions working except Instantiating preview pieces which are shown in the right side of the screen.
I have my codes separated into two: one for Tetris block spawner and one for Tetris block. Tetris block code is applied to each block.
I have a few problems right now.
(Whenever a Tetris block is placed, the first block in the preview is then played but the previews that have been instantiated before do not get destroyed, resulting in previews being instantiated on top of one another.) Edit : This is fixed
(I also get the "Index was outside the bounds of the array" error although I don't see why index goes out of bounds.) Edit : This is fixed
The "DeleteLine" function does not work now. It was working fine before I added preview of Tetris blocks.
I'm sorry if I'm asking a lot of question at once. I appreciate any help or hints.
Spawner Code:
public List<GameObject> Sevenbag;
public GameObject[] Baglist; // this is where i store prefabs
private int randomizer;
public GameObject[] CopyPrefab;
private static Vector2[] ColOneNext = new[] { new Vector2(13f, 16f),
new Vector2(13f, 12.5f),
new Vector2(13f, 9f),
new Vector2(13f, 5.5f),
new Vector2(13f, 2f) };
public GameObject CurrentTetrimino;
public bool PreviewUpdate = true;
// Start is called before the first frame update
void Start()
{
BagRandomizer();
NewTetrimino();
}
// Update is called once per frame
void Update()
{
PreviewInitializer();
BagRandomizer();
}
public void NewTetrimino()
{
CurrentTetrimino = Instantiate(Sevenbag[0], transform.position, Quaternion.identity);
CurrentTetrimino.GetComponent<TetrisBlock>().enabled = true;
Sevenbag.RemoveAt(0);
Destroy(GameObject.FindWithTag("PreviewTetrimino"));
PreviewUpdate = true;
}
public void PreviewInitializer()
{
if (Sevenbag.Count() > 10)
{
CopyPrefab = new GameObject[Sevenbag.Count()];
for (int i = 0; i <= 5; i++)
{
CopyPrefab[i] = Instantiate(Sevenbag[i], ColOneNext[i], Quaternion.identity) as GameObject; //this is where I get the Index outside the bounds of the array error
CopyPrefab[i].GetComponent<TetrisBlock>().enabled = false;
CopyPrefab[i].tag = "PreviewTetrimino";
PreviewUpdate = false;
}
}
else
{
BagRandomizer();
}
}
public void BagRandomizer()
{
while (Sevenbag.Count() <= 14)
{
List<int> randomint = new List<int>();
int randomizer;
for (int i = 0; i < 7; i++)
{
do
{
System.Random r = new System.Random();
randomizer = r.Next(0, 7);
} while (randomint.Contains(randomizer));
randomint.Add(randomizer);
Sevenbag.Add(Baglist[randomint[i]]);
Console.Write(Sevenbag[i]);
}
}
}
Tetris Block Code:
public static int height = 20;
public static int width = 10;
public static float fallTime = 0.8f;
private float previousTime;
private static Transform[,] grid = new Transform[width, height];
public Vector3 rotationPosition;
public float das_rate = 0.1f;
public float das_check = 0;
private bool CheckHold = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//codes of control of tetris blocks are deleted because they don't have any bug.
}
bool ValidMove()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height)
{
return false;
}
if (grid[roundedX, roundedY] != null)
{
return false;
}
}
return true;
}
void AddToGrid()
{
foreach (Transform children in transform)
{
int roundedX = Mathf.RoundToInt(children.transform.position.x);
int roundedY = Mathf.RoundToInt(children.transform.position.y);
grid[roundedX, roundedY] = children;
}
}
bool HasLine(int i)
{
for (int j = 0; j < width; j++)
{
if (grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i)
{
for (int j = 0; j < width; j++)
{
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i)
{
for (int y = i; y < height; y++)
{
for (int j = 0; j < width; j++)
{
if (grid[j, y] != null)
{
grid[j, y - 1] = grid[j, y];
grid[j, y] = null;
grid[j, y - 1].transform.position += new Vector3(0, -1, 0);
}
}
}
}
void CheckForLines()
{
for (int i = height; i >= 0; i--)
{
if (HasLine(i))
{
DeleteLine(i);
RowDown(i);
}
}
}
Answer by Llama_w_2Ls · Nov 02, 2020 at 10:51 AM
In your for loop
for (int i = 0; i <= 5; i++)
{
CopyPrefab[i] = Instantiate(Sevenbag[i], ColOneNext[i], Quaternion.identity) as GameObject; //this is where I get the Index outside the bounds of the array error
CopyPrefab[i].GetComponent<TetrisBlock>().enabled = false;
CopyPrefab[i].tag = "PreviewTetrimino";
PreviewUpdate = false;
}
You set i to be less than or equal to 5. This means that i can be 0, 1, 2, 3, 4, and 5. The value ColOneNext[5] does not exist since the array only consists of 5 elements and starts at the 0th index, meaning that ColOneNext[5] is referring to the 6th element in the ColOneNext array, which does not exist, and is therefore returning the Index is out of bounds error.
To fix this, change the line for (int i = 0; i <= 5; i++)
to for (int i = 0; i < 5; i++)
wow I don't know how I missed such simple mistake. Thank you very much for pointing that out. That error is gone now thanks to you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer
Can't seem to destroy instantiated objects. 1 Answer
Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers
Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 1 Answer
Proper/Best way to Instantiate and Destroy with Unity Networking? 2 Answers