Strategy Course (I got a problem with the GridDebugObjects)

Hello! Since MoveActionValidate part of the course i have problem with the GridDebugObjects after struggling to identify the problem,i just copied the code from the repos and still have the problem Here is the error code

NullReferenceException: Object reference not set to an instance of an object
GridDebugObject.Update () (at Assets/Scripts/Grid/GridDebugObject.cs:21)

Here is the script using System.Collections;

using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GridDebugObject : MonoBehaviour
{

    [SerializeField] private TextMeshPro textMeshPro;


    private GridObject gridObject;

    public void SetGridObject(GridObject gridObject)
    {
        this.gridObject = gridObject;
    }

    private void Update()
    {
        textMeshPro.text = gridObject.ToString();
    }

}

here is my GridObject script:

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

public class GridObject
{
    private GridSystem gridSystem;
    private GridPosition gridPosition;
    private List<Unit> unitList;

    public GridObject(GridSystem gridSystem, GridPosition gridPosition)
    {
        this.gridSystem = gridSystem;
        this.gridPosition = gridPosition;
        unitList = new List<Unit>();
    }

    public override string ToString()
    {
        string unitString = "";
        foreach (Unit unit in unitList)
        {
            unitString += unit + "\n";
        }
        return gridPosition.ToString() + "\n" + unitString;
    }

    public void AddUnit(Unit unit)
    {
        unitList.Add(unit);
    }

    public void RemoveUnit(Unit unit)
    {
        unitList.Remove(unit);
    }

    public List<Unit> GetUnitList()
    {
        return unitList;
    }

    public bool HasAnyUnit()
    {
        return unitList.Count > 0;
    }
}

And here is the image of my unity screen inside the prefab.Showing thats its correctly assigned.I am kind of a beginner been dabbling a in a lot of tutorial course but i am not finding the problem and is polluting my console when im trying to learn. Here plz help me!

Your error is on this line, textMeshPro.text = gridObject.ToString();

There are two things that could be null in this case… textMeshPro could be null if it is not set in the inspector, and gridObject could be null if it is not called when the GridDebugObject is spawned.

Most often, however, the cause of this is from when we construct the GridDebugObject and prefab it… Once it’s prefabbed, the GridDebugObject in the scene heirarchy needs to be deleted or the GridObject won’t be set on the orphanned GridDebugObject already in the scene when the game starts.

1 Like

Well i did not identify the problem,it can run smoothly even if the feature is enabled or disabled.Just that the 999+ error in the console are kind of annoying. i finished by just commenting out the line and now its fine.If i need it in the future i will just reenabled it!

image
See the one that doesn’t say “Clone”?
When you are out of play mode, it’s likely still there in the heirarchy. This is your noise maker. It wasn’t created at runtime and never got a handle to the underlying GridObject.

Yes this is the prefab i keep in the hierarchy if i take out this prefab i don’t see the number on the grid?So i should not have it in hierarchy and still get the debug number on the grid? Maybe i missed that then!I will go look closer at the episode we fiddle with that

The prefab you’re linking should be the one created when you drag that item to the Assets folder to make a prefab. If you’re linking the one in the scene, then, as I said, it will be noisy since it never had it’s GridObject assigned in LevelGrid.

i managed to stop the error.i needed to put the prefab from the Asset Folder and not the one in hierarchy thanks a lot ! Now i got a clean console to work with :smiley:

1 Like

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

Privacy & Terms