I have a bit of code (written in c#) that traverses a 6x6x6 array recursively and puts each item into a stack(using generic stacks of GameObjects) depending on the direction it was encountered from (i.e. if the x-value is increased it is put into the xStack)
The problem is that while it does run, It takes nearly a half an hour inside unity, and I have no idea why. Does unity have a problem with recursion in this fashion?
EDIT
It seems that the problem does not come from the recursion, but rather the hang is occurring when I try to output the contents of the stack to Debug.Log() using this bit of code.
while (xStack.Count > 0)
{
Debug.Log(xStack.Pop());
I've found the problem code, but i still don't know why it is acting this way..
original function
private GameObject match(int x = 0, int y = 0, int z = 0)
{
if (x + 1 < 6)
{
xStack.Push(match(x + 1, y, z));
}
if (y + 1 < 6)
{
yStack.Push(match(x, y + 1, z));
}
if (z + 1 < 6)
{
zStack.Push(match(x, y, z + 1));
}
return gemArray[x, y, z];
}
But your stack ends up with a size of 1389025 elements!!!!
– MortennobelSorry 132528 elements ... still a lot
– Mortennobelyeah, actually I was getting 811733....which was likely my issue, I've gotten it down to 21881 by restricting the checks to the faces, but the numbers are still much higher than I need...I'm thinking of creating a struct with the gameObject and three bool variables to show when traversal has already been completed on an index...I'm thinking that should cut down the number to the 456 I'm looking for.
– anon13683418I think I deserve a 'Correct answer' then ;-) At least my guess was quite close :-)
– MortennobelI think I agree.
– anon13683418