I am new to Unity. I have made the connection between the cell and 4 others surrounding its. After that i want to detect how many cells staying nearly have the same color. When i run its in the Editor, sometime it worked but sometimes it throw out StackOverflowException error.

Here is my code

Code
Code (CSharp):
void GetTotalCell(Cell init, Cell cell, CellColor color)
{
if(cell.topCell != null && init != cell.topCell)
{
if(cell.topCell.color == color)
{
//sameColor.Add(cell.topCell);
totalSameColor += 1;
GetTotalCell(cell, cell.topCell, color);
}
}

    if (cell.bottomCell != null && init != cell.bottomCell)
    {
        if (cell.bottomCell.color == color)
        {
            //sameColor.Add(cell.bottomCell);
            totalSameColor += 1;
            GetTotalCell(cell, cell.bottomCell, color);
        }
    }

    if (cell.rightCell != null && init != cell.rightCell)
    {
        if (cell.rightCell.color == color)
        {
            //sameColor.Add(cell.rightCell);
            totalSameColor += 1;
            GetTotalCell(cell, cell.rightCell, color);
        }
    }

    if (cell.leftCell != null && init != cell.leftCell)
    {
        if (cell.leftCell.color == color)
        {
            //sameColor.Add(cell.leftCell);
            totalSameColor += 1;
            GetTotalCell(cell, cell.leftCell, color);
        }
    }

    return;
}

void GetTotalCell()
{
    if(topCell != null)
    {
        if(topCell.color == color)
        {
            GetTotalCell(this, topCell, color);
            //sameColor.Add(topCell);
            totalSameColor += 1;
        }
    }

    if (bottomCell != null)
    {
        if (bottomCell.color == color)
        {
            //Debug.Log("bottomCell");
            GetTotalCell(this, bottomCell, color);
            //sameColor.Add(bottomCell);
            totalSameColor += 1;
        }
    }

    if (rightCell != null)
    {
        if (rightCell.color == color)
        {
            //Debug.Log("rightCell");
            GetTotalCell(this, rightCell, color);
            //sameColor.Add(rightCell);
            totalSameColor += 1;
        }
    }

    if (leftCell != null)
    {
        if (leftCell.color == color)
        {
            //Debug.Log("leftCell");
            GetTotalCell(this, leftCell, color);
            //sameColor.Add(leftCell);
            totalSameColor += 1;
        }
    }

    hasSet = true;
}

In the update function, i just call it one a time

private void Update()
    {
        if (board.IsInitBoard())
        {
            if (!hasSet)
            {
                GetTotalCell();
            }
        }
    }

board is the reference to the gameBoard in order to check the board whelther it finish or not.

So if i understand correctly you have a field of cells with like patches of different colors and you want to know how many cells each patch contains?


The StackOverflowException is easy to understand. You are calling GetTotalCell(…) from withing GetTotalCell(…). There’s no problem with that, but you have to make sure that you don’t fall into an infinite loop. Like this for example:

public void Start(){
    Eat();
}

public void Eat() {
    Debug.Log("I'm eating!");
    Eat();  //Can't stop eating!!!
}

Now in this code, we may be able to handle it. We are just eating really fast. But imagine what will happen if eating with two hands?

bool hasLeftHand = true;
bool hasRightHand = true;

public void Start(){
    Eat();
}

public void Eat()
{
    if(hasLeftHand)
    {
        Eat();
    }

    if(hasRightHand)
    {
        Eat();
    }
}

Each time we call the Eat function, it will call Eat twice from within it. Those Eat functions will call twice which will lead to 4 hands eating more and more… 8, 16, 32 hands… its a quadratic disaster. This is what is happening in your code.


You have to be really sure that you are not checking cells twice, because this will mean it is repeating. Maybe it might be better to create another function that cant loop back on itself. Or try another approach, like adding the cells to a List and checking the list to see if the cell is already in the group.