My Coordinates are all returning IsPlaceable = false and aren't the right colour

Hello, was doing my last lecture of the night but I feel like I’m slowly going crazy trying to understand the problem in my script. Bizarrely even after I copied all the contents from the course Waypoint.cs and CoordinateLabeler.cs files and overrode all of my code it was still having the same problem.

As the scene wasn’t updated for this lecture I couldn’t investigate further.

My Waypoint.cs code:

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

public class Waypoint : MonoBehaviour
{
    [SerializeField] GameObject towerPrefab;
    [SerializeField] bool isPlaceable;


    public bool IsPlaceable
    {
        get
        {
            return isPlaceable;
        }
    }

    MeshRenderer tileMeshRenderer;

    void Start()
    {
        tileMeshRenderer = GetComponentInChildren<MeshRenderer>();
    }

    void OnMouseDown()
    {
        if (isPlaceable)
        {
            Debug.Log(transform.name);
            Instantiate(towerPrefab, transform.position, Quaternion.identity);
            isPlaceable = false;
        }
    }

    void OnMouseOver()
    {
        if (isPlaceable)
        {
            setTileColor(2f, 2f, 2f, 1f);
        }
        else
        {
            setTileColor(3f, 1f, 1f, 1f);
        }
    }

    void OnMouseExit()
    {
        setTileColor(1f, 1f, 1f, 1f);
    }

    void setTileColor(float red, float green, float blue, float alpha)
    {
        Color tileColor = tileMeshRenderer.material.color;
        tileColor = new Color(red, green, blue, alpha);
        tileMeshRenderer.material.color = tileColor;
    }
}

My CoordinateLabeler.cs code:

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.red;

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

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

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

        ColorCoordinates();
    }

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

I can’t understand it - it allows me to Instantiate objects on the waypoint script, which is only possible if the tiles are ‘isPlaceable’ is true but when communicating that over to the CoordinateLabeler script, it treats every single tile as if ‘isPlaceable’ is false.

Just to clarify again, the issue is that the text isn’t turning white where tiles should be ‘Placeable’

Am I doing something silly here?

Hi!

What I’m seeing is that you haven’t set the isPlaceable variable as true anywhere in your code, so it will always be false, but you don’t need to set it as true in your code since you have it as an exposed variable, so you can set it as true in the inspector, perhaps you forgot to do that.

Good idea! I actually had forgotten to check that, however after looking into it, the only tile that has ‘IsPlaceable’ set to ‘true’ is the empty grass tile, which to my knowledge is correct.

So I’m wondering why this is changing to false?

Edit: I’d also like to say that the Waypoint.cs is reading IsPlaceable 100% correctly as I can tell with the highlight system. The roads (non placeable tiles) are showing as red and the empty grass tiles are showing green

Finally managed to work it out!

For anyone else who managed to get themselves into this bizarre situation, check that you don’t have a duplicate ‘Waypoint.cs’ script added to the ‘Text (TMP)’ child as well as the Tile prefab parent!

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

Privacy & Terms