Unable to make the debug key work

As far as I can tell, my code for both of the scripts is the same, but I can’t get the debug key working. I started with C, and then switched to Z, just in case, still nothing. Here is my script for the `[ExecuteAlways]
public class ComponentLabeler : MonoBehaviour
{
[SerializeField] Color defaultColor = Color.white;
[SerializeField] Color blockedColor = Color.gray;

TextMeshPro label;
Vector2Int coordinates = new Vector2Int();
Waypoint waypoint;

void Awake()
{
    label = GetComponent<TextMeshPro>();
    label.enabled = false;
    waypoint = GetComponentInParent<Waypoint>();
    DisplayCoordinates();
}

// Update is called once per frame
void Update()
{
    if(!Application.isPlaying)
    {
        DisplayCoordinates();
        UpdateObjectName();
    }

    ColorCoordinates();
    ToggleLabels();
}

void ToggleLabels()
{
    if(Input.GetKeyUp(KeyCode.Z))
    {
        Debug.Log("Debug mode enabled.");
        label.enabled = !label.IsActive();
    }
}

void ColorCoordinates()
{
    if(waypoint.IsPlaceable)
    {
        label.color = defaultColor;
    }
    else
    {
        label.color = blockedColor;
    }

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

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

}
`

Greetings.
I copied your script to my project and it worked. I guess I understood your mistake.
To use ToggleLabels method, you should jump to Play Mode. This script executing always, but ColorCoordinates() and ToggleLabels() methods works in play mode.

...
void Update()
{
    if(!Application.isPlaying)
    {
        DisplayCoordinates();      //It works in Edit Mode. In Play Mode, it displays whatever it has in edit mode.
        UpdateObjectName();       //It works in Edit Mode
    }

    ColorCoordinates();          //It works in Play Mode
    ToggleLabels();             //It works in Play Mode
}
...

Hope it helps.

By the way, It seems your script’s name is ComponentLabeler.
This script is labeling the tiles with coordinates. If you ask help about ComponentLabeler, there may be a misunderstanding. Pay attention to script names buddy! :slight_smile:

Solved my issue. I had two instances of my labeler on my tile prefab. Thus, when I was turning one off, I was immediately turning it back on.

I wouldn’t think the namespace should make too much of a difference, but I do have it named incorrectly. I was testing the debug controls in play mode. I have everything backed up on Github, just incase something screwy happens, which it usually does. I don’t know if I’m allowed to link to the repo or not, but someone will let me know one way or the other.

Hi @holyknight3,

I’m glad you solved the problem. And yes, you are allowed to share the link to your repo here. :slight_smile:


See also:

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

Privacy & Terms