[C#] Why Does My Physics.Raycast Return An "Object Reference Not Set To An Instance" Error?

Hey guys, I’m trying to build a path finding script and right now I’m try to fill out the graph. I’m in the stage where you figure out the size of the graph but I have ran into an issue. Currently I’m using a system where it runs down the coordinate plane, with each step using Physics.Checksphere to see if there are any objects around it, and then to use raycast to check its Southern and Eastern sides to see if the level’s walls are what they’re colliding with. If it is a wall, then its figured out its X component.

Some info about the game. It’s a 4 directional game and the game world is very very small. The rooms are boxes.

Here’s my code. The error is occuring at line 49. This is the error message:

And here’s my code.

using UnityEngine;
using System.Collections;

public class GraphGeneration : MonoBehaviour
{

    NodeScript[,] NodeGraph;

    bool maxY;
    bool maxX;

    int aphX; //"aph" means "Array Placeholder"
    int aphY;
    int gridSizeX;
    int gridSizeY;

    Vector3 position;
    bool walkable;
    RaycastHit hit;

    public GameObject placeholder;

    //Temp. piece of code saved
    //NodeGraph[aphX, aphY] = new NodeScript(walkable, position);

    void Awake()
    {

        //Temporary
        position = new Vector3(-0.79f, 0.59f, 0);
        NodeGraph = new NodeScript[100, 100];

        //This is where the graph size is found out
        while (maxY == false)
        {

            //First loops through the X plane to fill it out
            while (maxX == false)
            {

                //Used to move the position for the next node
                position.x += 0.01f;

                //Checks it surroundings to find any RoomWalls (where the graph should end)
                if (Physics.CheckSphere(position, 0.005f) == true)
                {

                    Physics.Raycast(position, new Vector3(1, 0, 0), out hit,0.01f);
                    if (hit.collider.tag == "RoomWall") { maxX = true; break; }

                }

                gridSizeX++;
                aphX++;

                //Used to visualize the graph
                GameObject.Instantiate(placeholder);

            }
           
            //Same as before
            position.y += 0.01f;

            Physics.Raycast(position, new Vector3(0, -1, 0), out hit,0.01f);
            if (hit.collider.tag == "RoomWall") { maxY = true; break;}

            aphY++;
            gridSizeY++;

            GameObject.Instantiate(placeholder);

        }

    }

}

I can’t figure out why its giving me this error for the life of me! Thanks for your attention!

Have you checked if the hit.collider is null?

1 Like

It is. I was wondering “Well why isn’t it picking up on anything” then I realized that its not picking up on anything, but I’m saying it is by not excluding the portion of code if its null. Thanks!

One usually put the Raycast in an if statement, because the method returns true if the ray hits anything. You are skipping this logic.

1 Like