Coordinate labels are not changing color

I have typed out the code exactly as gary has and in the exploring neighbors lecture when i test play the labels all stay the same color visual studio shows no errors unity shows no errors the scripts are all attached to their appropriate game objects yet its not working

This is almost never the case. Show us your code and we can get to the source of your discomfort

i figured out that i left the gridSize at 0, 0 however after i changed it now the only label that changes color is the one i set in the PathFinder script for the current search node coordinates in the inspector and it only changes to the pathColor even if its not a path tile

heres my coordinate labeler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;

[ExecuteAlways]
[RequireComponent(typeof(TextMeshPro))]
public class CoordinateLabeler : MonoBehaviour
{
[SerializeField] Color defaultColor = Color.white;
[SerializeField] Color blockedColor = Color.gray;
[SerializeField] Color exploredColor = Color.yellow;
[SerializeField] Color pathColor = new Color(1f, 0.5f, 0f);

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();
    }

    SetLabelColor();
    ToggleLabels();
}

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

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 DisplayCoordinates()
{
    coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
    coordinates.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);
    label.text = coordinates.x + "," + coordinates.y;
    if (!Application.isPlaying)
    {
        label.enabled = true;
    }
}

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

heres my GridManager

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

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);
            
        }
    }
}

}
heres my PathFinder

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathFinder : MonoBehaviour
{
[SerializeField] Node currentSearchNode;
Vector2Int directions = {Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down};
GridManager gridManager;
Dictionary<Vector2Int, Node> grid;

void Awake()
{
    gridManager = FindObjectOfType<GridManager>();
    if (gridManager != null )
    {
        grid = gridManager.Grid;
    }
}

void Start()
{
    ExploreNeighbors();
}

void ExploreNeighbors()
{
    List<Node> neighbors = new List<Node>();

    foreach (Vector2Int direction in directions)
    {
        Vector2Int neighborCoordinates = currentSearchNode.coordinates + direction;

        if (grid.ContainsKey(neighborCoordinates))
        {
            neighbors.Add(grid[neighborCoordinates]);

            // remove after testing
            grid[neighborCoordinates].isExplored = true;
            grid[currentSearchNode.coordinates].isPath = true;
        }
    }

    
}

}

So, I tested your code and it works as expected. The node in currentSearchNode gets highlighted as a path because the Pathfinding script sets it to a path. This is for testing only. It also highlights the neighbours (if it has valid neighbours) with the explored color

Tile at (2, 1) is highlighted in orange (path) and the 4 neighbours are highlighted in yellow (explored)

i dont understand why its working for you but not me would the version of unity make a difference?

i dont know if it helps but if i remove the lines tagged with // remove after testing then it just does nothing at all

It shouldn’t make a difference. There’s really nothing here that would be different across versions. What did you set the colors to be? And what are your grid size and current search node values?

Yes, those are the ‘testing’ lines. The ones that set the currentSearchNode to a path and the neighbours to explored. Removing those lines will cause nothing to happen.

here is what my colors are set too and something i didnt notice about the screenshot you posted is your label actually turns orange and mine is red

[SerializeField] Color defaultColor = Color.white;
[SerializeField] Color blockedColor = Color.gray;
[SerializeField] Color exploredColor = Color.yellow;
[SerializeField] Color pathColor = new Color(1f, 0.5f, 0f);

If yours is red it means you changed the colors in the inspector. These colors in the code are no longer being used. What are the colors in the inspector?

i dont have any colors in the inspector

You do. Check the Tile prefab where the CoordinateLabeler script is. It is clear from the screenshot that the colors have been changed.

ok so i didnt have the CoordinateLabeler attached to my tile prefab but when i attached it it destroyed my environment by placing all the tiles on 0,0

You had one on the tiles that was in the previous screenshot. That’s how you got red and black labels. Undo until your scene is back to normal

cant undo i added to CoordinateLabeler to the prefab and saved it the script was not previously on the prefab and going back through the lectures i dont think gary ever mentioned putting the labeler on the tile prefabs being why we ( and i might be wrong on this) added the [ExecuteAlways] attribute to the script so i believe my only fix at this point is just to rebuild the world

The script is on the TextMeshPro text object in the prefab. ExecuteAlways doesn’t execute scripts that are not on game objects.

Your screenshot showed your scene as not saved so you may have been able to salvage it. Probably too late now.

You have a CoordinateLabeler on your tile. The colors of that labeler has been changed. I suspect the default color and the ‘explored’ colors are the same and this is why you can’t see the highlighted neighbors

yep im an idiot lol i was looking on the prefab parent object and not the textmeshpro but to finally answer your question no the colors were not changed in the inspector

I see that here, but your running game has colors that do not match this, which means it changed somewhere. Your default color is black, and your path color is red. Perhaps you used a prefab variant where these colors have been changed, but they are definitely different. If you want, you could zip it and I could look for it.

There’s also an exception in your console. Can you open the console and click on it? Perhaps it’s related, or perhaps not. Either way, we could possible fix it

the exception really doesnt effect the game at all as its associated with this line
coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x); in the DisplayCoordinates() method of the CoordinateLabeler and only pops up when i exit play mode. I backed up my project from the first part of the section so im going to just start the second part over again because whatever mistake i made is so subtle that its apparently undetectable thank you for your time and if after doing it over i still run into issues you may hear from me again (though i hope not) and at least you helped me see it wasnt an issue with the code but something i must have done in unity

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

Privacy & Terms