Issue with NullReferenceException

So I’m trying to make a script where if the player hits a Question block the the model swaps out with an empty block version but this error keeps popping up
image
The question block script:

using System.Collections;
using System;
using UnityEngine;
using Unity.VisualScripting;

public class Questionblockscript : MonoBehaviour
{
    Rigidbody qblock;
    
    public float testnumber;
    public bool Isempty;
    public bool Hassomething;

    private Vector3 velocity;
    private void Awake()
    {
        qblock = GetComponent<Rigidbody>();

        Isempty = false;
    }

    void Update() 
    {
       
    }


    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            if (Hassomething == false) {
                Destroy(gameObject);
            }
            else {
                Isempty = true;
            }
        }
    }
}

The full block model script:

using UnityEngine.InputSystem.Interactions;

public class Fullblockvisibility : MonoBehaviour
{
    public Questionblockscript Block;
    Renderer visible;

    void Update()
    {
        if (Block.Isempty == true) {
            visible.enabled = false;
        }
        else {
            visible.enabled = true;
        }
        
    }
}


The empty block model script:

using UnityEngine;
using UnityEngine.InputSystem.Interactions;

public class Emptyblockvisibility : MonoBehaviour
{
    public Questionblockscript Block;
    Renderer visible;

    void Update()
    {
        if (Block.Isempty == true) {
            visible.enabled = true;
        }
        else {
            visible.enabled = false;
        }
        
    }

    void Start() {
        Block.testnumber += 10;
    }
}

As you can see I tried to do some debug with the .testnumber which works as intended so the error is caused I assume because of the if statement for reasons I don’t know. Even more strangely the error for the full block script points to line 15 but the empty block script error points to line 11.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

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

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

Update: the testnumber script doesn’t work now either and it has the same error, I have no idea what I did to break it.

  • if (Block.Isempty == true) { and Block.testnumber += 10;
  • No idea, the latter was working before but now it doesn’t
  • I don’t know how

The method I’m using is also the second one used in the video below, I also tried the 3rd one but it has the same error.

A null reference exception means you’re trying to access something that isn’t there.

There is nothing that would be assigning these Renderer visible component references, therefore they will be null. You should serialize them so you can assign references in the inspector.

None of this…

sounds anything like the three steps above.

As Spiney observes, your visible variable is almost certainly not initialized. Go fix that.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

You should slow down and rethink the problem. I’ve not watched the video you referenced but if they show anything like this as a solution you should stop watching it.

This is a classic example of “do not do this” architecture. Why? Because you will get confused and it ultimately will not work. I can’t determine if you are interested but I’ll take a chance and mention some of the problems.

Unneeded, unused using statements
Atypical naming of classes and properties
Empty methods
Complete duplication of Fullblockvisibility and Emptyblockvisibility scripts.

It appears that “Renderer visible” is private has anything been assigned? And naming it visible is a mistake.

The assignment in the Update is a one-liner

	visible.enabled = Block.Isempty;

You haven’t actually assigned an initial value to Block.testnumber (this isn’t required but might be considered good form). The value is 0f in any case. When you increment by 10 it would be clearer to use float notation 10f

My advice is to slow down and clean up what you have. You will end up with so much spaghetti that it no longer makes any sense.

1 Like

It works now! At first I didn’t know what I would use the inspector for but turns I need to just drag the object itself into the renderer field. (Also testnumber works again for some reason)

Thanks guys.

1 Like