Broke something bad, Stack overflow

Was attempting to call UpdateGridVisual to clear on unit death and I somewhere broke something that I am getting a memory leak, it is tied to the Unit and Selecting them as SelectionVisual quit working and Action buttons don’t populate.

Had a rough night and having trouble thinking this morning. Booted with Diags but still not sure what I changed to cause error.

here is code

StackOverflowException: The requested operation caused a stack overflow.
System.String.CreateStringFromEncoding (System.Byte* bytes, System.Int32 byteLength, System.Text.Encoding encoding) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
System.Text.Encoding.GetString (System.Byte* bytes, System.Int32 byteCount) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
System.Text.Encoding.GetString (System.ReadOnlySpan`1[T] bytes) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
System.String.Ctor (System.SByte* value, System.Int32 startIndex, System.Int32 length, System.Text.Encoding enc) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
System.String.CreateString (System.SByte* value, System.Int32 startIndex, System.Int32 length, System.Text.Encoding enc) (at <3dd5df5ef4974f29afeb2d3ba227c5da>:0)
(wrapper managed-to-managed) System.String..ctor(sbyte*,int,int,System.Text.Encoding)
UnityEngine.StackTraceUtility.ExtractStackTrace () (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
UnityEngine.DebugLogHandler.LogFormat (UnityEngine.LogType logType, UnityEngine.Object context, System.String format, System.Object[] args) (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
UnityEngine.Logger.Log (UnityEngine.LogType logType, System.Object message) (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
UnityEngine.Debug.LogError (System.Object message) (at <790a43858b4b404fbe02a2b7b4a0d9d6>:0)
UnitActionSystem.GetSelectedUnit () (at Assets/Game/Scripts/Controls/UnitActionSystem.cs:121)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:116)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVisualType) (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:108)
GridSystemVisual.UpdateGridVisual () (at Assets/Game/Scripts/Grid/GridSystemVisual.cs:138)
GridSystemVisual.ShowGridPositionList (System.Collections.Generic.List`1[T] gridPositionList, GridSystemVisual+GridVisualType gridVis<message truncated>```

Let’s see the code for GridSystemVisual.UpdateGridVisual() and GridSystemVisual.ShowGridPositionList()

They are calling each other, so let’s have a look. Issue is very likely in GridSystemVisual.ShowGridPositionList()

1 Like
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridSystemVisual : MonoBehaviour
{
    [SerializeField] private Transform gridSystemVisualSinglePrefab;

    [Serializable] public struct GridVisualTypeMaterial
    {
        public GridVisualType gridVisualType;
        public Material material;
    }
    
    private GridSystemVisualSingle[,] gridSystemVisualSingleArray;
    [SerializeField] private List<GridVisualTypeMaterial> gridVisualTypeMaterialList;


    public static GridSystemVisual instance;

    public enum GridVisualType { White, Blue, Red, RedSoft, Yellow }

    private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;
    }

    private void Start()
    {
        gridSystemVisualSingleArray = new GridSystemVisualSingle[
            LevelGrid.instance.GetWidth(), 
            LevelGrid.instance.GetHeight()];

        for (int x = 0; x < LevelGrid.instance.GetWidth(); x++)
        {
            for (int z = 0; z < LevelGrid.instance.GetHeight(); z++)
            {
                GridPosition gridPosition = new GridPosition(x,z);
                Transform GridSystemVisualSingleTransform = 
                    Instantiate(gridSystemVisualSinglePrefab, 
                    LevelGrid.instance.GetWorldPosition(gridPosition), 
                    Quaternion.identity);

                gridSystemVisualSingleArray[x,z] = GridSystemVisualSingleTransform.GetComponent<GridSystemVisualSingle>();
            }
        }

        UnitActionSystem.instance.OnSelectedActionChanged += UnitActionSystem_OnSelectedActionChanged;
        LevelGrid.instance.OnAnyUnitMovedGridPosition += LevelGrid_OnAnyUnitMovedGridPosition;

        UpdateGridVisual();
    }

    private void Update()
    {
        UpdateGridVisual();
    }


    public void HideAllGridPosition()
    {
        for (int x = 0; x < LevelGrid.instance.GetWidth(); x++)
        {
            for (int z = 0; z < LevelGrid.instance.GetHeight(); z++)
            {
                gridSystemVisualSingleArray[x, z].Hide();
            }
        }
    }

    private void ShowGridPositionRange(GridPosition gridPosition, int range, GridVisualType gridVisualType)
    {
        List<GridPosition> gridPosList = new List<GridPosition>();


        for (int x = -range; x <= range; x++)
        {
            for (int z = -range; z <= range; z++)
            {

                GridPosition testGridPosition = gridPosition + new GridPosition(x,z);

                if (!LevelGrid.instance.IsValidGridPosition(testGridPosition)) continue;

                int testDistance = Mathf.Abs(x) + Mathf.Abs(z);
                if (testDistance > range) continue;

                gridPosList.Add(testGridPosition);
            }
        }
        UpdateGridVisual();

        ShowGridPositionList(gridPosList, gridVisualType);
    }

    public void ShowGridPositionList(List<GridPosition> gridPositionList, GridVisualType gridVisualType)
    {
        foreach (GridPosition gridPosition in gridPositionList)
        {
            gridSystemVisualSingleArray[gridPosition.x, gridPosition.z].Show(GetGridVisualTypeMaterial(gridVisualType));
        }
        UpdateGridVisual();

    }

    public void UpdateGridVisual()
    {
        HideAllGridPosition();

        Unit selectedUnit = UnitActionSystem.instance.GetSelectedUnit();


        BaseAction selectedAction = UnitActionSystem.instance.GetSelectedAction();
        if (selectedAction == null) return;
        GridVisualType gridVisualType = GridVisualType.White;

        switch (selectedAction)
        {
            default:
            case MoveAction moveAction:
                gridVisualType = GridVisualType.White;
                break;
            case ShootAction shootAction:
                gridVisualType = GridVisualType.Red;
                ShowGridPositionRange(selectedUnit.GetGridPosition(), shootAction.GetMaxShootDistance(), GridVisualType.RedSoft);
                break;
            case SpinAction spinAction:
                gridVisualType = GridVisualType.Blue;
                break;
        }
        
        ShowGridPositionList(
            selectedAction.GetValidActionGridPositionList(), gridVisualType);


    }

    private Material GetGridVisualTypeMaterial(GridVisualType gridVisualType)
    {
        foreach (GridVisualTypeMaterial gridVisualTypeMaterial in gridVisualTypeMaterialList)
        {
            if (gridVisualTypeMaterial.gridVisualType == gridVisualType)
            {
                return gridVisualTypeMaterial.material;
            }
        }
        Debug.LogError("Couldn't find GridVisualTypeMaterial for GridVisualType " + gridVisualType);
        return null;
    }

    private void UnitActionSystem_OnSelectedActionChanged(object sender, EventArgs e)
    {
        UpdateGridVisual();
    }

    private void LevelGrid_OnAnyUnitMovedGridPosition(object sender, EventArgs e)
    {
        UpdateGridVisual();
    }

}

Yup. There it is. You can’t call UpdateGridVisual() here.

Just some more of what’s happening: ShowGridPositionList(...) calls UpdateGridVisual() there. But then UpdateGridVisual() calls ShowGridPositionList(...)

so, ShowGridPositionList(...) calls UpdateGridVisual() which then calls ShowGridPositionList(...) which then calls UpdateGridVisual() which then calls ShowGridPositionList(...), etc. Eventually, Stack Overflow Exception

2 Likes

Yep, that fixed it thanks. Don’t know how many times I double checked against the resources and missed that line was different.

A stack overflow is almost always

  • two methods calling each other (it may be more, but circular), or a function calling itself (which is ok if you do recursive logic and know what you are doing),
  • or an endless loop.

The error you got above showed which two methods are calling each other, and where

GridSystemVisual.UpdateGridVisual - Line 138

and

GridSystemVisual.ShowGridPositionList - Line 108

You just need to figure out which one should not be calling the other one.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms