Tilebased game. Calculate available spaces to go

I have a problem with my code. The engine just crashes. I dont get an error. I dont get anything. It is probably because it is stuck in a loop of some sort. So maybe someone in the community could see the problem before i do.

It is a tile based game and i am trying to calculate the spaces where a unit can go.

private void SetWalkableTiles(GameObject unit)
    {
        UnitStats unitStats = unit.GetComponent<UnitStats>();
        pointList = new List<PointPath>();
        int x = (int) unit.transform.position.x / 10;
        int z = (int) unit.transform.position.z / 10;
        int steps = unitStats.GetCurrentSteps();

        if (steps > 0)
        {
            AddPoint(x + 1, z, x, z, steps, unitStats);
            AddPoint(x - 1, z, x, z, steps, unitStats);
            AddPoint(x, z + 1, x, z, steps, unitStats);
            AddPoint(x, z - 1, x, z, steps, unitStats);
        }
    }

  public void AddPoint(int posX, int posZ, int fromX, int fromZ, int currentCost, UnitStats unitStats)
    {
        int cost = unitStats.FromTagToTilecost(gameHandler.GetModelTag(posX, posZ));
        if (cost != null)
        {
            int costAfter = currentCost - cost;
            Debug.Log("Cost After: " + costAfter);
            if (costAfter > 0)
            {
                AddToPointList(posX, posZ, currentCost - cost);

                if ((posX - 1) != fromX)
                {
                    AddPoint((posX - 1), posZ, posX, posZ, costAfter, unitStats);
                }
                if ((posX + 1) != fromX)
                {
                    AddPoint((posX + 1), posZ, posX, posZ, costAfter, unitStats);
                }
                if ((posZ + 1) != fromZ)
                {
                    AddPoint(posX, (posZ + 1), posX, posZ, costAfter, unitStats);
                }
                if ((posZ - 1) != fromZ)
                {
                    AddPoint(posX, (posZ - 1), posX, posZ, costAfter, unitStats);
                }
            }
        }
    }

So a littlebit about the code. When a tile is clicked. The application will know if a unit is on that tile. If a unit is on that tile. The application will call the function SetWalkableTiles(). That function will call a data script that has all important information about that unit. If the unit has steps to walk. The function will call on my failed recursive function.

The recursive function AddPoint() will try to walk all the available spaces on every direction but back the same way it came. If it finds a walkable spot. It will try to add that spot to a list if it is not added (but if it cost less. it will replace the other one in the list)

Tell me if you want more information. Any help is appreciated. If you know a better way that i could create this recursive function could help aswell

There is clearly a condition that is causing you to stack overflow (forever recursion inside your AddPoint method).

If you don’t have access to an IDE that you can just put breakpoints I would put a bunch of Debug.log() calls in different areas inside AddPoint that will print out what exactly is the condition that is causing it to crash.
Then you can check the output log to see what the condition was.

Honestly though, if you don’t absolutely HAVE to have a recursive function here, I would avoid them unless it really is THE WAY to handle this functionality.
The reason is that OK you found one issue here that causes it to eat dirt, but what if there are other hidden conditions that you don’t find… but that users do. Now what? They will REALLY have no idea what happened, and therefore you won’t either and will be very difficult to fix. Alternatively, I might suggest to greatly simplify the recursive section more: to isolate the recursivity into its most basic form so you can ensure you have all conditions handled inside of it to never hang.
Hope this helps.