Hi there
I’m a beginner with Unity programming and currently trying to create a funvtional Tetris clone. So far, everything I wanted works. I’m working right now on the script that should delete the full lines and move the ones above downwards. So here are the scripts I have for that. The function checkLines() is called out of the update() function:
//check, if there is a full Line
function checkLines(){
var line : int;
for (var a=19;a>=0;a--){
for(var b=0;b<10;b++){
if(board[b,a]==1) line++;
}
if (line>=10){
deleteLines(a);
moveLines(a);
}
line=0;
}
}
//delete the full line
function deleteLines(a:int){
for(var X=0;X<10;X++){
if(blocksLevel[X,a]){
Destroy(blocksLevel[X,a].gameObject);
board[X,a]=0;
}
}
}
//move lines above the deleted
function moveLines(a:int){
for(var Y=a+1;Y<20;Y++){
for(var X=0;X<10;X++){
if(blocksLevel[X,Y]){
blocksLevel[X,Y].transform.position.y -=2.2;
blocksLevel[X,Y].name = "block"+X+a;
board[X,Y]=0;
board[X,a]=1;
}
}
}
}
This script works exactly as I want it to work as long there is just one single line to delete. As soon as there are 2 to 4 lines, the results are kinda random meaning that sometimes not all blocks get deleted or not all blocks are moved down one unit.
To me it looks like that unity is “jumping” over some lines of code because the update function is called to soon. Is that possible? Is there a problem with my code? What is causing my problem?
I’m be really thankful if you could help me
Thanks
mitti2000
Edit: I changed the script to a version without GameObject.Find calls as suggested by Yokimato