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?