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?
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.
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
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).