UnassignedReferenceException even though I did assign the object

I wanted it so when you interact with a lightswitch it would set a point light active, however even when I assign the light I still get the error.

public class InteractionScript : MonoBehaviour
{
    private bool doorCollision;
    private bool radioCollision;
    private bool sinkCollision;
    private bool slipperCollision;
    private bool lightCollision;
   
    private bool openDoor = false;
   
    private bool lightIsOn;
    private bool slipperIsOn;
    private bool radioIsOff1;
    private bool radioIsOff2;
    private bool waterWasDrank;
   
    private Collider collider1;
   
    public GameObject cup;
    public GameObject slipper;
    public GameObject hallwayLight; //This is the light that should become active

    public Text hallwayTask;
    public Text slipperTask;
    public Text waterTask;
    // Update is called once per frame
   
    void Update()
    {
      
       if (doorCollision && Input.GetKeyDown("e"))
       {
            collider1.gameObject.transform.Rotate(0f, 180f, 0f);
            openDoor = true;
       }
       if (lightCollision && Input.GetKeyDown("e")) //The statement that should activate the light
       {
            hallwayLight.SetActive(false);
            lightIsOn = true;
       }
       if (slipperCollision && lightIsOn && Input.GetKeyDown("e"))
       {
              slipper.SetActive(false);
              slipperIsOn = true;
       }
       if (radioCollision && slipperIsOn && Input.GetKeyDown("e"))
       {
            radioIsOff1 = true;
            Debug.Log("Radio is now off");
       }
       if (sinkCollision && radioIsOff1 && Input.GetKeyDown("e"))
       {
            cup.SetActive(true);
            waterWasDrank = true;
       }
        if (radioIsOff1)
        {
            slipperIsOn = false;
            radioIsOff2 = true;
        }
   
   
    }
   
}

I know the interaction works because when I interact with the sink the cup becomes active fine, so I have absolutely no clue what the problem is. I wrote a debug.log statement for when I interacted with the light to make sure that the interaction wasn’t the problem, and it worked fine. And I also tried switching the light for other gameobjects like cubes and whatnot, but that did not work either. I tried accessing the light by writing

hallwaylight = gameobject.findobjectwithtag("light")

but that didnt work either Any help would mean the world to me since this project has been smooth sailing until this problem . Thank you!

Then you didn’t find the one that you failed to assign… you’re still on step #1. Keep after it.

The answer is always the same… ALWAYS!

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 ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Yeah but I already know what is null and i’m on step 2 right now. I have no clue why it is null it makes no sense since I clearly assigned it in the inspector.

Here’s the error I got:
UnassignedReferenceException: The variable hallwayLight of InteractionScript has not been assigned.
You probably need to assign the hallwayLight variable of the InteractionScript script in the inspector.

You clearly assigned the one you’re looking at.

Are there others in the scene / prefab?

Well according to the error its the hallway light that’s unassigned even though all the public gameobjects in the script were assigned.

Then you are definitely still on Step #1.

Unless… something is setting it to null later.

The complete checklist is already linked above. I’m not retyping each one again here.

Umm heres a vid so its easier to understand

9203745--1283931--videogamevid1.gif
9203745--1283934--1260042-1c62fa948b26b7bec0e006c317869734.gif

As Kurt said there’s probably another instance of this component running around. Use the second overload of Debug.Log so that when you click on the error, it highlights the object that threw the error.

Mind you this script seems to be handling too many things at once. It looks like it should be broken down into smaller scripts with individual, singular responsibilities.

Umm, here’s a three-step checklist so it’s easier to understand:

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

You will discover that the exception is remarkably patient.

The exception is quite happy to simply sit and wait until you find it.

That’s just how they are. They’re kinda weird that way.

Woah I never knew debug.log could do that. I will definitely be checking that out and I do plan on cleaning up my code
Thank you!

I found the problem! I had two interaction scripts in two different parts which caused in interference I guess. Thank you so much to everyone that helped!