NullReferenceException error, but it doesn't stop the code from working

image

As you can see above, it still runs and sometimes seems to have no issues, but flags an error repeatedly that I don’t understand. Looking through the other people’s issues, I checked with those and found that mine was an issue that wasn’t fixed in the same way. After several hours of trying to fix it, I ended up copy pasting all of the code from the example finished version word for word and still got the same issue. I went through and re-attached each script to each object and put each reference object where it needed to be. I feel like I’m going crazy, but I cannot find any reason it’s doing this, nor why it isn’t actually causing a single problem with the function of the code.




image

For future code pastes, please paste in the text, formatting the text of the script following the instructions below. This makes the code easier for us to read and copy to quote, check, and edit.

In Update, only one of two things can be null… either the gridObject or the textMeshPro.

private void Update()
{
    if(gridObject==null) 
    {
         Debug.Log($"GridDebugObject at {transform.position} has a null gridObject.");
         return;
    }
    if(textMeshPro == null)
    {
         Debug.Log($"TextMeshPro on GridDebugObject at {gridObject.GetGridPosition()} is null");
    }
}

Now in CreateDebugObjects, let’s make a small change in CreateDebugObjects

GridDebugObject gridDebugObject = debugTransform.GetCopmnent<GridDebugObject>();
GridObject gridObject = GetGridObjects(gridPosition);
if(gridObject==null)
{
    Debug.Log($"GridSystem gridObject at {gridPosition} is null");
}
gridDebugObject.SetGridObject(gridObject);

Apologies for the improper structure before, is the following how the code should be formatted?

GridSystem.cs

public void CreateDebugObjects(Transform debugPrefab)
    {
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                GridPosition gridPosition = new GridPosition(x, z);

                Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(gridPosition), Quaternion.identity);
                GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>();
                GridObject gridObject = GetGridObject(gridPosition);
                if (gridObject == null)
                {
                    Debug.Log($"GridSystem gridObject at {gridPosition} is null");
                }
                gridDebugObject.SetGridObject(gridObject);
            }
        }
    }

The above code for some reason gets me the warning "The referenced script on this Behaviour (Game Object ‘GridDebugObject’) is missing!

The below code gives me the following error: Assets\Scripts\Grid\GridDebugObject.cs(30,71): error CS0117: ‘GridObject’ does not contain a definition for ‘GetGridPosition’.

If I put either in the wrong spot, please let me know.

GridDebugObjects.cs

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

        if (gridObject == null)
        {
            Debug.Log($"GridDebugObject at {transform.position} has a null gridObject.");
            return;
        }
        if (textMeshPro == null)
        {
            Debug.Log($"TextMeshPro on GridDebugObject at {GridObject.GetGridPosition()} is null");
        }
    }

That is the correct way to format the text.

For the Update() in GridDebugObjects.cs, put the null check lines before the textmeshpro.text = gridObject.toString() or the method will stop when either is null and never get to the null check lines, meaning you still won’t know which one was null.

That is an odd message to get at this point… Can you show which line is throwing the error? (find the line number and paste in the line in question)

Ah, it will eventually have a GetGridPosition() method…

Add this to GridObject class:

public GridPosition GetGridPosition()=>gridPosition;

Of course, as I check to find the line that leads to the first warning it just goes away, but now that I’ve put the GetGridPosition line in, I’m getting the following error:

Assets\Scripts\Grid\GridDebugObject.cs(28,60): error CS0120: An object reference is required for the non-static field, method, or property ‘GridObject.GetGridPosition()’

Sorry this is being so difficult.

It looks like you capitalized GridObject.GetGridPosition(), meaning that you were referring to a (nonexistent) static method. Make sure it’s gridObject.GetGridPosition() which represents the gridObject variable in the GridDebugObject method.

Ah yeah, that would do it. It ran, and now I’ve got the one that’s the problem apparently.
image

The issue now though is that I have no idea why it’s an issue, or how to fix it.


I got the warning again! It doesn’t actually give me any information at all, though.

Oh, this makes a little more sense. Does the GridDebugObject have a GridDebugObject script on it?

It does, is it not supposed to?
image

Ok, that’s actually interesting… I think I may know what’s going on…
If you take a look at your GameScene without playing, you’ll note that there is a GridDebugObject in the scene already. That’s the GridDebugObject that you turned into a prefab. This should be removed. Make sure your Testing is linked to the GridDebugObject that is the Prefab in the Assets folder.

It is supposed to… I’m not sure now why there is an issue with the referenced script error, but it should actually go away when you delete the GridDebugObject from the scene heirarchy.

THANK YOU SO MUCH!
That was indeed the issue. I’d assumed that was supposed to be there, apparently incorrectly. You’ve been an incredible help!

I’m glad I could help!

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

Privacy & Terms