Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by SwaggyMcChicken, Dec 7, 2016.

  1. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GraphGeneration : MonoBehaviour
    5. {
    6.  
    7.     NodeScript[,] NodeGraph;
    8.  
    9.     bool maxY;
    10.     bool maxX;
    11.  
    12.     int aphX; //"aph" means "Array Placeholder"
    13.     int aphY;
    14.     int gridSizeX;
    15.     int gridSizeY;
    16.  
    17.     Vector3 position;
    18.     bool walkable;
    19.     RaycastHit hit;
    20.  
    21.     public GameObject placeholder;
    22.  
    23.     //Temp. piece of code saved
    24.     //NodeGraph[aphX, aphY] = new NodeScript(walkable, position);
    25.  
    26.     void Awake()
    27.     {
    28.  
    29.         //Temporary
    30.         position = new Vector3(-0.79f, 0.59f, 0);
    31.         NodeGraph = new NodeScript[100, 100];
    32.  
    33.         //This is where the graph size is found out
    34.         while (maxY == false)
    35.         {
    36.  
    37.             //First loops through the X plane to fill it out
    38.             while (maxX == false)
    39.             {
    40.  
    41.                 //Used to move the position for the next node
    42.                 position.x += 0.01f;
    43.  
    44.                 //Checks it surroundings to find any RoomWalls (where the graph should end)
    45.                 if (Physics.CheckSphere(position, 0.005f) == true)
    46.                 {
    47.  
    48.                     Physics.Raycast(position, new Vector3(1, 0, 0), out hit,0.01f);
    49.                     if (hit.collider.tag == "RoomWall") { maxX = true; break; }
    50.  
    51.                 }
    52.  
    53.                 gridSizeX++;
    54.                 aphX++;
    55.  
    56.                 //Used to visualize the graph
    57.                 GameObject.Instantiate(placeholder);
    58.  
    59.             }
    60.            
    61.             //Same as before
    62.             position.y += 0.01f;
    63.  
    64.             Physics.Raycast(position, new Vector3(0, -1, 0), out hit,0.01f);
    65.             if (hit.collider.tag == "RoomWall") { maxY = true; break;}
    66.  
    67.             aphY++;
    68.             gridSizeY++;
    69.  
    70.             GameObject.Instantiate(placeholder);
    71.  
    72.         }
    73.  
    74.     }
    75.  
    76. }
    77.  
    I can't figure out why its giving me this error for the life of me! Thanks for your attention!
     
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    319
    Have you checked if the `hit.collider` is null?
     
    JakovHStudios likes this.
  3. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    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!
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    One usually put the Raycast in an if statement, because the method returns true if the ray hits anything. You are skipping this logic.
     
    TaleOf4Gamers likes this.