Recursive function do stack overflow (problem with static var)

I’m porting a game from as3 to unity and i got a problem with a function, in as3 I got that

private function breakBlock(x,y){
var disp:int=_block.length;
for(var db=0;db<disp;db++){
if(_block[db].posX==x && _block[db].posY==y){
_canvas.removeChild(_block[db]);
_block.splice(db,1);
setTimeout(function(){breakBlock(x,y-1)}, 200);
setTimeout(function(){breakBlock(x,y+1)}, 200);
setTimeout(function(){breakBlock(x-1,y)}, 200);
setTimeout(function(){breakBlock(x+1,y)}, 200);

}
}
}

_block is an array with movie clips like GameObject[ ] destruibles = GameObject.FindGameObjectsWithTag(“breakables”);

i do that on unity
public void breakBlock(int killx,int killy){
GameObject[ ] breakables= GameObject.FindGameObjectsWithTag(“breakables”);
foreach (GameObject breackable in breakables) {
if(breackable.GetComponent().breackcordx==killx&& breackable.GetComponent().breackcordx==killy){
Destroy(breackable);
breakBlock(killx-1,killy);
}
}
}

if i do
breakBlock(killx-1,killy);breakBlock(killx+1,killy); or breakBlock(killx-1,killy); or breakBlock(killx+1,killy);
works correctly but with
breakBlock(killx+1,killy);breakBlock(killx-1,killy);
the game crashes

how can i store breackables on the class out of the function and acces/modify it from the function?

ok… nameOfClass.breackable.Lenght
:eyes:
but storing breackables out of the function and reloading on enter keep crashing

Unless _block.length becomes zero as part of that code it’ll recursive call this method forever.

I thought it was dying cause if(_block[db].posX==x && _block[db].posY==y) dont lanunch anything if adjacent block doesnt exist

Ah yes, I missed that condition - sorry.

Have you put some debug statements in there to ensure that those conditions are being met?

I suspect they are not ever being met. How many executions do you expect? put debug statements in there to see what’s happening.

Looking again I think that it’s not possible for all 4 of those recursive calls to all end (some will never have inputs that match your x & y condition).

eg: breakBlock(x,y-1) will never match that condition.

My unity freezes before send any output.

I dont know if i undeestand completly your words

whatch this at 0:42
(

)

im trying to do something like the destroyable walls, on as3 works prefect , if breakBlock doesnt match, nothing happen, and function follow while match but in unity fails

public static GameObject[ ] breakables;

public void breakBlock(int killx,int killy){
level.breakables=GameObject.FindGameObjectsWithTag(“breakables”);
print(level.breakables.Length);
foreach (GameObject breackable in level.breakables) {
if(breackable.GetComponent().breackcordx==killx&& breackable.GetComponent().breackcordx==killy){
Destroy(breackable);
breakBlock(killx-1,killy);
}
}
}

print(level.breakables.Length); show the same number while the function is working

Objects/Components aren’t actually destroyed until the end of Update(), so I imagine FindGameObjectsWithTag() will continue to loop over the same entire set of breakable objects. In other words ,two breakable blocks are repeatedly try to destroy each other because they’re adjacent and still in the scene.

Unity advises against using DestroyImmediate() so instead, try adding a public bool broken = true; to your Breakable class, and setting breackable.broken = true; before you Destroy it. Then, only process the block if it’s not broken: if(!breackable.broken && breackable.GetComponent.().breackcordx==killx&& breackable.GetComponent().breackcordx==killy).