Null Reference Exception

Hi,

I have an error in my Console after clicking ‘Play Game’ on the scene ‘Start’. Is in some ways similar issue to the one that Steven have (here is a link).

Here is a Console output:

The mentioned ‘Level.cs’ code lines 26-30:

    public void LoadGame()
    {
        SceneManager.LoadScene("Game");
        FindObjectOfType<PlaySession>().ResetGame();
    }

The code in ‘PlaySession.cs’ (in a unity tutorial the name for this file is GameSession):

    public void ResetGame()
    {
        Destroy(gameObject);
    } 

I’m not sure what is wrong with that section. Any ideas?

Thanks
RR

Hi Romario,

Welcome to our community! :slight_smile:

NullReferenceException means that a reference (“link”) to an instance is missing. Is there a PlaySession object in your scene?

Yes, it is.

Here is an screen grab from ‘Game’ scene hierarchy:

error-GameScene-Hierarchy-14-05-20202_StarFire-V1.1

Here is a screen grab from ‘Game’ scene ‘Play Session’ inspector:

error-GameScene-Inspector-PlaySession-14-05-20202_StarFire-V1.1

RR

Thank you. That looks fine so far. :slight_smile:

Is your PlaySession object a “singleton”? If so, check if you have gameObject.SetActive(false); in the same code block as the Destroy method.

Nina,

I have a singleton in in ‘PlaySession’ that is added to Play Session object:

    private void Awake()
    {
        ScreenScore();
    }

but don’t have a gameObject.SetActive(false); added to ‘RestedGame’ destroy method.

I will add it and check if that’s help.

RR

Updating ‘ResetGame’ to:

    public void ResetGame()
    {
        gameObject.SetActive(false);
        Destroy(gameObject);
    } 

didn’t help :frowning:

RR

That isn’t the “singleton”.

The ‘ScreenScore’ is a singleton. Here is a rest of the code:

    private void Awake()
    {
        ScreenScore();
    }

    private void ScreenScore()
    {
        int numberOfSessions = FindObjectsOfType<PlaySession>().Length;

        if (numberOfSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

It’s based on the unity course, section 115 (31_id_cud), You can watch video. I didn’t use method name ‘SetUpSingleton’. Used my own naming convention.

RR

I meant this potential fix:

        if (numberOfSessions > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }

Thanks Nina, but this solution didn’t help. The error is still showing.

I think I forgot to mentioned that the error appears when I click ‘Play Game’ at the beginning. I mean when I run the game preview from ‘Start’.

RR

In which scene do you press the play button? In the Start scene? And then you click on Play Game? If so, double click on the error message in your console.

Yes, I click the ‘Play Game’ button in the ‘Start’ scene. If I double click the error message in my VS I have highlighted this line:

        FindObjectOfType<PlaySession>().ResetGame();

line in 'Level.cs"

Here is the rest of the error message:

UnityEngine.Events.InvokableCall.Invoke () (at <480508088aee40cab70818ff164a29d5>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <480508088aee40cab70818ff164a29d5>:0)
UnityEngine.UI.Button.Press () (at D:/App/Unity/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at D:/App/Unity/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at D:/App/Unity/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at D:/App/Unity/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at D:/App/Unity/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

I’m not a c# developer (yet).

But the only point where a “null exception” can occur is when invoking “ResetGame()” on something that is “null”.

Could you please try the following:
print(FindObjectOfType<PlaySession>());

and check wether it prints something other than null

I have the same issue. Debug.Log(FindObjectOfType< PlaySession>()); returns null

Issue was resolved by putting a PlaySession Prefab into the starting scene.

4 Likes

Good that you have found the problem.

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

Privacy & Terms