Console Error in script CoordinateLabler

I have an error in the console which says - CoordinateLabeler.cs(60,33): error CS1061: ‘GridManager’ does not contain a definition for ‘GetNode’ and no accessible extension method ‘GetNode’ accepting a first argument of type ‘GridManager’ could be found (are you missing a using directive or an assembly reference?)

I have also gone through Gary’s script and that didn’t fix the error either

cs(60,33)- Node node = gridManager.GetNode(coordinates);

HERE’S MY SCRIPT

[ExecuteAlways]
[RequireComponent(typeof(TextMeshPro))]
public class CoordinateLabeler : MonoBehaviour
{
    [SerializeField] Color defaultColor  = Color.white;
    [SerializeField] Color blockedColor = Color.grey;
    [SerializeField] Color exploredColor = Color.blue;
    [SerializeField] Color pathColor = Color.cyan;

    TextMeshPro label;
    Vector2Int coordinates = new Vector2Int();

    GridManager gridManager;
    
    

     void Awake()
    {
        gridManager = FindObjectOfType<GridManager>();
        label = GetComponent<TextMeshPro>();
        label.enabled = false;
        DisplayCoordinates();
    }
    void Update()
    {
        if (!Application.isPlaying)
        {
            DisplayCoordinates();
            UpdateObjectName();
            label.enabled = true;

        }

        SetLabelColor();
        ToggleLabels();
    }

    void DisplayCoordinates()
    {
        coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
        coordinates.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.x);
        label.text = coordinates.x + "," + coordinates.y;
    }

    void UpdateObjectName()
    {
        transform.parent.name = coordinates.ToString();
    }

    void SetLabelColor()
    {
        if (gridManager == null)
        { return; }

        Node node = gridManager.GetNode(coordinates);

        if (node == null)
        { return; }

        if (!node.isWalkable)
        {
            label.color = blockedColor;
        }
        else if (node.isPath)
        {
            label.color = pathColor;
        }

        else if (node.isExplored)
        {
            label.color = exploredColor;
        }

        else
        {
            label.color = defaultColor;
        }

    }

    void ToggleLabels()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            label.enabled = !label.IsActive(); 
        }
    }
}```

The problem is likely in GridManager.cs. Can you also post that code?

I just started working on the project again and there’s no errors now, but can you still go though the script incase i missed something

public class GridManager : MonoBehaviour
{
  [SerializeField] Vector2Int gridSize;
  Dictionary<Vector2Int , Node> grid = new Dictionary<Vector2Int, Node>();

     void Awake()
    {
        CreateGrid();   
    }

    public Node GetNode(Vector2Int coordinates)
    {
        if (grid.ContainsKey(coordinates))
        {
            return grid[coordinates];
        }
        return null;    
    }

    void CreateGrid()
    {
        for (int x = 0; x < gridSize.x; x++)
        {
            for (int y = 0; y < gridSize.y; y++)
            {
                Vector2Int coordinates = new Vector2Int(x, y);
                grid.Add(coordinates, new Node(coordinates , true));
                Debug.Log(grid[coordinates].coordinates + "=" + grid[coordinates].isWalkable);
            }
        }
    }
}

:+1: Looks good

1 Like

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

Privacy & Terms