Why Do I get objectexceptionnull error for a raycast when i click on an object

Im trying to make clickable menu buttons i tested the script at tafe and it worked perfectly the buttons worked fine but wheni loaded it into my actual game project it came up with this error this is the script
this is the settings to switch between the menu Buttons and the settings UI

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;

public class Setting : MonoBehaviour
{
    public GameObject canvas;
    public GameObject buttons;
    public GameObject back;  // Name of the scene to load
    void Start()
    {
         Lol();
    }

    void Update()
    {
        // Check for mouse click
        if (Input.GetMouseButtonDown(0))
        {
            // Shoot a ray from the mouse position into the scene
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            // Check if the ray hits an object
            if (Physics.Raycast(ray, out hit))
            {
                // Check if the hit object is the object with this script attached
                if (hit.collider.gameObject == gameObject)
                {
                    Toggle();
                   
                }
            }
        }
    }
    void Toggle()
    {
        back.SetActive(true);
        canvas.SetActive(true);
        buttons.SetActive(false);
    }
    void Lol()
    {
           back.SetActive(false);
        canvas.SetActive(false);
        buttons.SetActive(true);
    }
}

I think the reason is that you’re checking the hit.collider.gameObject directly on the collider. But if the ray hits nothing, the collider property itself will be null, and null doesn’t have a gameObject property.

That shouldn’t be the case as it’s only used when the Raycast call returns true so the RaycastHit will be valid.

The problem with this thread is that it’s not stated what part of the code is throwing the exception here, it’s left for devs to guess. It could well be in the Toggle or Lol methods because those fields are not initialized.

1 Like

Thats what i was trying to find out Because it wasnt given me a line or anything But i found out that OnMouseDown Was Much Easier

My main point was that you never stated what part of your script was giving you the exception. Sounds like an Null exception to me.

The raycast The Null Error Only happened when i click on said cube And i would get two of the same errors

That doesn’t say where in your script the exception was thrown, it states an action. When you click you call a method “Toggle”. The only code that could throw a null exception here is you accessing one of those three fields if they were NULL.

The point is that if you need help, you need to provide some detail. Showing the script doesn’t provide all the information required to help you.

Idk It didnt give me Any Info on where the null was should i make it a Bool?

Attach a debugger then it’ll stop on the place where the exception is thrown. As you tagged your thread, you need to debug it.

I don’t know what that means.

Ill try that if it works It works if not Its fine i can use OnMouseDown