-1

I have two scripts. The TileManager script is attached to the first sprite object Tile A (I've commented where the error occurs):

using UnityEngine;
using System.Collections;
using System.Text;
using System;
using UnityEngine.SceneManagement;

public class TileManager : MonoBehaviour 
{
    public ScoreAction addScore;
......
    addScore.DummyMethod();  // Causes "NullReferenceException: Object reference not set to an instance of an object"

The ScoreAction script is attached to a TextMeshPro text object called Score:

using UnityEngine;
using System.Collections;
using System.Text;

public class ScoreAction : MonoBehaviour 
{
    [SerializeField] private GameObject Score;
    
......

    public void DummyMethod ()
    {
        Debug.Log ("In ScoreAction");
    }

I've tried different approaches to this, but just can't get around this obstacle.

Pezza
  • 23
  • 1
  • 6
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Considering the `ScoreAction.DummyMethod ` method does not depend on the `Score` field, `TileManager.addScore` is _obviously_ `null` –  Nov 07 '21 at 04:50
  • Thanks for the answer. I realise that a null is being passed, but I don't know why it should occur. The ScoreAction script is intended to display the score after calculating what it should be, depending on the argument passed in. DummyMethod will become the method intended to represent that calculation and display. (I hope I'm not being obtuse here.) – Pezza Nov 07 '21 at 05:17
  • Have you attached the script "addScore" to the TileManager script from the editor? – Swagrim Nov 07 '21 at 06:04
  • I'm sorry, Swagrim, I don't understand the question. I have a Score game object to which I've attached the ScoreAction script. What is suspicious is that the variable field simply says Score. I can't figure out why that is, but it may be the cause of my problem. – Pezza Nov 07 '21 at 06:24
  • Try as I may, I could not drag the gameobject to the public field in the script in Inspector, so I've abandoned the idea of a separate script and will handle it all in one script. Thanks for all the help. – Pezza Nov 08 '21 at 04:30

1 Answers1

0

The NullReferenceException usually happens when you are trying to access a class, or an instance of a class that is null.

In your example, the error comes from addScore.DummyMethod(). You are trying to access methods from addScore instance. So if you want to check if that instance is null or not, simply just using Debug.Log(), like this

Debug.Log(addScore == null)

I think your problem is that you haven't dragged the gameobject contain the ScoreAction script to the public field addScore

  • I've now fixed this. The problem was caused by my literal interpretation of "drag game object to the variable in the script in Inspector". I assumed it meant the script displayed in the Inspector, which didn't work because the script is not editable. I assumed also that the script should be editable in the Inspector, which I eventually discovered was not the case. That made me look at the various game objects in the Inspector more closely, and that's when I noticed that right at the bottom of the Controller object associated with my script was a mention of Score Label. It was set to None. – Pezza Nov 09 '21 at 03:58