Labels do not appear in edit mode and are not toggleable

The tile labels disappear and do not toggle if the C key is pressed while in Play Mode. Seems like a bug. Here’s my code:

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

[ExecuteAlways]
public class CoordinateLabeler : MonoBehaviour
{
    [SerializeField] Color defaultColor = Color.white;
    [SerializeField] Color blockedColor = Color.gray;

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

    private void Awake()
    {
        label = GetComponent<TextMeshPro>();
        label.enabled = false;

        waypoint = GetComponentInParent<Waypoint>();
        DisplayCoordinates();

    }
    private void Update()
    {
        if (!Application.isPlaying)
        {
            DisplayCoordinates();
            UpdateObjectName();

        }

        ColorCoordinates();
        ToggleLabels();
    }

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

    void ColorCoordinates()
    {
        if (waypoint.IsPlacable)
        {
            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();
    }
}

Any help is appreciated; thanks!

I tested your code and it worked fine. How did you set these up? I pasted it into a script and set it up based on the transform.parent code.

These are attached to the Text (TMP) game object in the tile prefab. I’ve attached a screenshot.

I had them setup exactly like that. Does the text show when you are not in play mode? I can see it’s disabled at the moment but if you enable it, does it show? Like I said, the code works so I am just trying to figure out if there is something else wrong

If I enable the component, the text does appear for that tile. But will disappear when I go into play mode.

The text does not show in play mode until I hit the C key. If I press the play button again, the labels deactivate. The C key does not work in edit mode.

I’ve shared a video on my google drive in the link below:
https://drive.google.com/file/d/1bSPqTQ58ojhHZavkR9r1cglblKm3SbnY/view?usp=sharing

In the video I am pressing the C key while in edit mode. I then press play and continue to press the C key. I then press the play button again while the label text is displayed to show the labels deactivate.

That’s the correct behaviour. The user input does not work in edit mode.

Ah, I see. That seems a little odd that you wouldn’t be able to see or toggle the tile labels when in edit mode; but if that’s the design, then I guess it’s a feature. :sweat_smile:

I guess it’s okay since the tiles are labeled in the hierarchy, so basically this issue is NAB.

Oh, I didn’t realise it was edit mode that wasn’t working for you. I have to read headings more carefully

Like Nina said, this is expected behaviour.

Not really. When you create a game (edit mode) you will be typing a lot. It would be a little odd if the game starts ‘doing things’ while you are typing names, or setting values in the inspector.

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

Privacy & Terms