I am in the relem rush part of the course. I wrote the code for the coordinate system but its showing an error message “NullReferenceException: Object reference not set to an instance of an object” which means some instance of an object in code is not set to its reference in the editor.
But my code is running as expected and I am experiencing no problems.
PLEASE HELP!
This is the error
NullReferenceException: Object reference not set to an instance of an object
CoordinateSystem.displayCoordinates () (at Assets/Scripts/CoordinateSystem.cs:27)
CoordinateSystem.Update () (at Assets/Scripts/CoordinateSystem.cs:20)
This is the code
using UnityEngine;
using TMPro;
using System.Reflection.Emit;
[ExecuteAlways]
public class CoordinateSystem : MonoBehaviour
{
Vector2Int coords;
TextMeshPro label;
void Awake()
{
label = GetComponent<TextMeshPro>();
}
void Update()
{
if (!Application.isPlaying)
{
displayCoordinates();
updateName();
}
}
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 updateName()
{
transform.parent.name = "( "+ coords.x + ", " + coords.y + " )";
}
}