Why use the MeshRenderer instead of the GameObject

Instead of using the GridSystemVisualSingle I just deactivated the gameobject completely. Is this less performant? Or is there any other reason to use the meshrenderer?

  public void HideAllVisuals() {
        foreach (KeyValuePair<GridPosition,Transform> a in gridSystemVisuals) {
            a.Value.gameObject.SetActive(false);
        }
    }
    
    public void ShowGridPositionList(List<GridPosition> gridPositions) {
        foreach (GridPosition p in gridPositions) {
            gridSystemVisuals[p].gameObject.SetActive(true);
        }
    }

Also I stored the visuals based on the GridPositions in a dictonairy. This way I can easily walk thru all visuals without using the width and height. And all the allowed gridpositions are generated by the GridSystem so the double for-loop is only inside the GridSystem.

1 Like

Sure that works, the object just has a MeshRenderer so enabling/disabling the game object will work pretty much the same. In terms of performance both are likely to perform pretty much the same, so use whatever approach you prefer.

One benefit of having a dedicated class to handling each Grid Position Visual is in case in the future you want to do something special with the visual other than just show/hide, maybe you want to animate it in some way or something. In that case having a dedicated script would help keep the code more organized.

For the dictionary, sure that technically works but personally I don’t like it.
The grid has a Rectangular shape so having a “Rectangular” data type to store it makes more sense to me.
If you have an invalid GridPosition like -1,-1 you might get some strange behaviour because that could be a valid Key whereas in an array you would just get an out of range error right away.

2 Likes

Privacy & Terms