Issue with the lables

Everything in this code is working except the toggleLabel fucntion. Can someone please help me fix it?
Im pressing C but the labels are not toggling on and off

using UnityEngine;
using TMPro;

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

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

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

    void Update()
    {
        if (!Application.isPlaying)
        {
            displayCoordinates();
            updateName();
        }

        colorCoordinates();
        toogleLabels();
    }

    void colorCoordinates()
    {
        if (waypoint.IsPlacable)
        {
            label.color= defaultColor;
        }
        else if (!waypoint.IsPlacable)
        {
            label.color= blockedColor;
        }
    }

    void displayCoordinates()
    {
        coords.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
        coords.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);

        label.text = coords.x + ", " + coords.y;
    }

    void toogleLabels()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            label.enabled = !label.IsActive();   // If label is already enabled, set label to the opposite of whatever the present state is
        }
        
    }

    void updateName()
    {
        transform.parent.name = "( "+ coords.x + ", " + coords.y + " )";
    }
}

Hi,

In which course and lecture are you?

When do you expect the toggleLabels() method to work? Generally, user input gets processed at runtime only.

Im in Lecture 120 of Complete C# Unity Game Developer 3D course. I basically need to be able to toggle the textMeshPro component of tiles during runtime

But its always invisible in my situation

Try maybe
(Input.GetKey(KeyCode.C))
Instead of
(Input.GetKeyDown(KeyCode.C))

@Willrun, Input.GetKey(KeyCode.C) returns true as long as the C key is being pressed down. With that condition, label.enabled = !label.IsActive(); would get executed each frame as long as the C key is being pressed down.

@Rustic_Dude, have you already tried to add Debug.Logs to your code to see what is going on during runtime? Does your if-block get executed? Which object is referenced by label?

label.enabled = !label.IsActive();
Think should be
label.enabled = !label. enabled ;

To toggle its state
But @Nina is right of course adding console. logs would help tell where you problem is

Thank you! I will try this

I dunno what exactly happened but somehow my TMpro component was turned off in my prefab. Now its working as intended. THANK YOU SOO MUCH!!

@Nina @Willrun

I’m glad you found and solved the problem. :slight_smile:

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

Privacy & Terms