Unknown error

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 + " )";
    }
}

Your Vector2Int variable needs to be initialized:

Vector2Int coords = new Vector2Int();

Im still getting the same error. I actually removed that initialisation and saw if it solved the error but it didn’t.

But I will try again tho

Hi Rustic_Dude,

First of all, remove the superfluous namespace from your code to prevent any conflicts.

Since Vector2Int is a struct, and since structs are value types, coords cannot be null.

Please double click on the error message. To which line in your code does it refer? Which of the variables in that line could be null? What value does label have at runtime?

If I were to hazard a guess, I’d say the object has no parent…

Its showing an error on the 27th line
There might be an error while accessing the parent of the object

Maybe because the parent changes name/label in the next part of code

Surprisingly, the code works as intended even WITH the error so i think tis might be a glich

It does have a parent but its label changes with the position (As outlined in the course itself) so thats its easier to keep a track of it via code

image

This is how my hierarchy looks. The script is on the TMP Text

This is the error

I checked the code and it’s fine. Perhaps you have another object somewhere in the hierarchy with the script on it. It will probably be top-level

Yeah lol
I just checked the Hierarchy and realised that i had added the script to the PARENT as well :sweat_smile::joy: :joy:

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

Privacy & Terms