Search Unity

Raycast "Object reference not set to an instance of an object"

Discussion in 'Scripting' started by mothstardust, Mar 15, 2019.

  1. mothstardust

    mothstardust

    Joined:
    Mar 11, 2019
    Posts:
    2
    Hey all - I'm new to Unity and I'm poring over old threads but I still can't figure out my problem! Could someone help me out? I know the error is on line 35, and I've looked over the documentation - but I'm not sure what I'm missing. What object isn't being referenced?

    Using: Unity 2018.3.8
    Error: NullReferenceException: Object reference not set to an instance of an object
    clickMoveDropScript.Update () (at Assets/Scripts/clickMoveDropScript.cs:35)

    Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class clickMoveDropScript : MonoBehaviour
    6. {
    7.  
    8.     public bool moveableObjectGrabbed = false;
    9.  
    10.     Ray moRay;
    11.     public Transform moTransform;
    12.     public LayerMask whatIsMoveableObject;
    13.     RaycastHit moHit;
    14.  
    15.     public LayerMask whatIsGround;
    16.     public Transform ground;
    17.     RaycastHit groundHit;
    18.  
    19.     public Transform mousePosMarker;
    20.     RaycastHit mousePosHit;
    21.     public float moYOffsetFromGround = 0f;
    22.     public float mousePosYOffsetFromGround = 0f;
    23.     public Vector3 mousePosRelToGround;
    24.  
    25.     // Start is called before the first frame update
    26.    void Start() {
    27.         moHit = new RaycastHit();
    28.         groundHit = new RaycastHit();
    29.         mousePosMarker.gameObject.SetActive(false);
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update() {
    34.  
    35.         moRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    36.  
    37.         if (Input.GetMouseButtonDown(0))
    38.     {
    39.         FindAndGrabMoveableObject();
    40.     }
    41.         if (Input.GetMouseButtonUp(0))
    42.         {
    43.             DropMoveableObject();
    44.         }
    45.  
    46.         moveableObjectGrabbed = moTransform != null;
    47.         mousePosMarker.gameObject.SetActive(moveableObjectGrabbed);
    48.  
    49.         if (moveableObjectGrabbed)
    50.         {
    51.             TraceMousePosRelToGround();
    52.         }
    53.  
    54. }
    55.  
    56.     //Find And Grab
    57.     void FindAndGrabMoveableObject() {
    58.         if (Physics.Raycast(
    59.             moRay,
    60.             out moHit,
    61.             Mathf.Infinity,
    62.             whatIsMoveableObject))
    63.         {
    64.         moTransform = moHit.transform;
    65.         moTransform.GetComponent<Rigidbody>().isKinematic = true;
    66.         FindGroundBelowMoveableObject();
    67.         }
    68.     }
    69.  
    70.     //Find ground
    71.    void FindGroundBelowMoveableObject() {
    72.         if(Physics.Raycast(
    73.             moTransform.position,
    74.             Vector3.down,
    75.             out groundHit,
    76.             Mathf.Infinity,
    77.             whatIsGround))
    78.  
    79.         {
    80.         ground = groundHit.transform;
    81.         }
    82.     }
    83.  
    84.     //Trace Position Relative to ground
    85.    void TraceMousePosRelToGround () {
    86.     if(Physics.Raycast(
    87.             moRay,
    88.             out mousePosHit,
    89.             Mathf.Infinity,
    90.             whatIsGround))
    91.         {
    92.         mousePosRelToGround = mousePosHit.point;
    93.         moTransform.position = new Vector3(
    94.             mousePosRelToGround.x,
    95.             mousePosRelToGround.y + moYOffsetFromGround,
    96.             mousePosRelToGround.z);
    97.  
    98.         mousePosMarker.position = new Vector3(
    99.             mousePosRelToGround.x,
    100.             mousePosRelToGround.y + mousePosYOffsetFromGround,
    101.             mousePosRelToGround.z);
    102.         }
    103.  
    104. }
    105.  
    106.     //DropMoveableObject
    107.     void DropMoveableObject () {
    108.     if (moTransform != null)
    109.         moTransform.GetComponent<Rigidbody>().isKinematic = false;
    110.         moTransform = null;
    111.         ground = null;
    112.     }
    113. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
  3. mothstardust

    mothstardust

    Joined:
    Mar 11, 2019
    Posts:
    2
    That was the issue!! Thankfully just a minor fix. Thanks so much, everything is working great now :)