Hello. Been following along the Realm Rush section. Working on the grid and setting that up.
Had it working, closely resembling Gary’s. Had the Null Reference exception once prior, but that was because I tried simply apply it by sliding it onto the text, but it applied the script to the cube.
After fixing my error, it showed the coordinates and was working correctly until I tried to do the “play” test he did to see what would happen. After that, the coordinates were broken. Found out that it disabled the Script. But turning it on now gives a Null Reference Exception.
NullReferenceException: Object reference not set to an instance of an object
Coordinates.DisplayCoords () (at Assets/Scripts/Coordinates.cs:29)
Coordinates.Update () (at Assets/Scripts/Coordinates.cs:21)
is the error and location is line 29:coords.text = coordinates.x + " , " + coordinates.y;
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using TMPro; //Gives access to the text mesh pro asset
5 using System;
6
7 [ExecuteAlways]
8 public class Coordinates : MonoBehaviour{
9 // Start is called before the first frame update
10
11 TextMeshPro coords;
12 Vector2Int coordinates = new Vector2Int();
13
14 void Awake(){
15 coords = GetComponent<TextMeshPro>();
16 }
17
18
19 void Update() {
20 if(!Application.isPlaying){
21 DisplayCoords();
22 }
23 }
24
25 void DisplayCoords(){
26 coordinates.x = Mathf.RoundToInt(transform.parent.position.x);
27 coordinates.y = Mathf.RoundToInt(transform.parent.position.y);
28
29 coords.text = coordinates.x + " , " + coordinates.y;
30 }
31 }
I’m not seeing why it’s Null Referencing.