Issue updating SerializedField color variable of tile labels

I’m encountering a really weird issue with my text labels in the Realm Rush tutorial. I’ll include my code and a screenshot of the scene view for reference.

In my CoordinateLabeler script I had initially initialized my defaultColor and blockedColor to be white and gray respectively as in the tutorial. I then wanted to change the blockedColor to black. So, at the top of my script I switched the intialization from
[SerializeField] Color blockedColor = Color.gray;
to
[SerializeField] Color blockedColor = Color.black;

However, back in unity this change has not occured in the inspector. The blocked color is still gray. No matter what color I change this to, it is always gray. I decided to try and change the variable name to otherColor and make it red. This did work. However now when I try to change the initialization to any other color, it is always red!

It seems like I can initialize my color variables to some color at first and they work, but are then stuck that color forever.
I can resolve this by removing the [SerializedField] tag so it must be something up with the inspector. I double checked that none of my gameobjects have had the inspector field manually changed to override the color.

I’ve never encountered this with any other [SerializedField] variable before, ints, floats, strings, ect all work fine. Only seeing this with colors for some reason. Any help understanding what might be going on would be very helpful. Thanks!

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

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

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

    private void Awake()
    {
        label = GetComponent<TextMeshPro>();
        waypoint = GetComponentInParent<Waypoint>();
        DisplayCoordinates();
        //label.color = Color.red;
    }

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

        ColorCoordinates();
    }

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

    void ColorCoordinates()
    {
       if (waypoint.IsPlaceable)
        {
            label.color = defaultColor;
            Debug.Log("turn white!");
        }
        else
        {
            label.color = blockedColor;
            Debug.Log("turn black!");
        }
    }
}

This is how serialization works. The field of this object instance is saved to the scene and changing the code will not make any difference because the value has been serialized and stored. When you write code pick a reasonable default, but after that you have to change the values in the inspector. Values changed in the inspector are the values that are stored. You had blockedColor as grey. That was stored. Now you change the color in the script to black. Doesn’t matter. The grey has been serialized and will only change if you set the blockedColor to black in the inspector

2 Likes

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

Privacy & Terms