Null Reference Exception

private GameObject blackScreen;
    private GameObject paper;
    private bool interacting;

    private void Start()
    {
        blackScreen = GameObject.Find("Black Screen");
        blackScreen.SetActive(false);

        paper = GameObject.Find("Paper");
        paper.SetActive(false);

        Debug.Log(blackScreen.name); // Works
        Debug.Log(paper.name); // Also works
    }

The script prints out the name of the objects, so it clearly did find the objects, why do I get the error?

(Error is on line 8)

What error? Post the entire error message (not paraphrasing it like your topic title). And Include the whole script (as this is not everything as there is no class or using statements).

The full error is
[22:18:04] NullReferenceException: Object reference not set to an instance of an object Interactions.Start () (at Assets/Scripts/Interactions.cs:17
And the full code is

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

public class Interactions : MonoBehaviour
{
    private GameObject blackScreen;
    private GameObject paper;
    private bool interacting;

    private void Start()
    {
        blackScreen = GameObject.Find("Black Screen");
        blackScreen.SetActive(false);

        paper = GameObject.Find("Paper");
        paper.SetActive(false);
    }

    public void ReadPapers()
    {
        if ( interacting )
        {
            BasicVariables.pausePlayer = false;
            Cursor.lockState = CursorLockMode.Locked;

            blackScreen.SetActive(false);
            paper.SetActive(false);

            interacting = false;
        }

        else
        {
            BasicVariables.pausePlayer = true;
            Cursor.lockState = CursorLockMode.None;

            blackScreen.SetActive(true);
            paper.SetActive(true);

            interacting = true;
        }
    }
}

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: Regarding GameObject.Find · UnityTipsRedux

I will accept this as a guilty plea that you didn’t read the @StarManta link I provided above.

Why should I provide another link that you also won’t read?

Sorry, I was a bit busy and skimmed over your reply. After reading them, they are very helpful, thank you.