Search Unity

Raycast garb error: NullReferenceException

Discussion in 'Scripting' started by SawyerK, Dec 29, 2019.

  1. SawyerK

    SawyerK

    Joined:
    Feb 11, 2016
    Posts:
    55
    I have this script for picking up and throwing objects and i get this error for a couple of lines.

    Line: 15, 54, 55, 54 ,57,

    NullReferenceException: Object reference not set to an instance of an object
    RaycastPickup.Update () (at Assets/Scripts/RaycastPickup.cs:19)

    If i remove these lines, i don't get the errors, but i cant release the object that i grabbed, it stocks with the tempParent and just starts floating away because the gravity not set back to true.
    Is there a way to fix this? So i can use the distance and everything.

    Code (CSharp):
    1. public class RaycastPickup : MonoBehaviour
    2. {
    3.  
    4.     float throwForce = 300;
    5.     RaycastHit hit;
    6.     GameObject item;
    7.     public GameObject tempParent;
    8.     bool isHolding = false;
    9.     float distance;
    10.     Vector3 objectPos;
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
    16.         if (distance >= 4f)
    17.         {
    18.             isHolding = false;
    19.         }
    20.  
    21.         if (Input.GetMouseButton(0) && Physics.Raycast(transform.position, transform.forward, out hit, 4) && hit.transform.GetComponent<Rigidbody>())
    22.         {
    23.             item = hit.transform.gameObject;
    24.  
    25.             if (isHolding == true)
    26.             {
    27.                 isHolding = false;
    28.             }
    29.  
    30.  
    31.             else
    32.                 if (distance <= 4f)
    33.                 {
    34.                 isHolding = true;
    35.                 item.GetComponent<Rigidbody>().useGravity = false;
    36.                 }
    37.  
    38.         }
    39.         if (isHolding == true)
    40.         {
    41.             item.GetComponent<Rigidbody>().velocity = Vector3.zero;
    42.             item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    43.             item.transform.SetParent(tempParent.transform);
    44.  
    45.  
    46.             if (Input.GetMouseButtonDown(1))
    47.             {
    48.                 item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
    49.                 isHolding = false;
    50.             }
    51.         }
    52.         else
    53.         {
    54.             objectPos = item.transform.position;
    55.             item.transform.SetParent(null);
    56.             item.GetComponent<Rigidbody>().useGravity = true;
    57.             item.transform.position = objectPos;
    58.         }
    59.     }
    60. }
    61.  
     
  2. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    131
    on line 15 you are trying to get a distance from a GameObject named "item" but you are not setting this variable anywhere in the code, so unity cant find it.
    "Object reference not set to an instance of an object"
    this error means that unity doesn't find an object on the lines that the error points to

    you need to set this variable reference somewhere
     
  3. SawyerK

    SawyerK

    Joined:
    Feb 11, 2016
    Posts:
    55
    So line 23 isn't a reference for this? After i click mb0 the raycast hits the gameobject and that would be the reference.
     
  4. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    131
    line 23 happens after the first missing reference
    also you should declare "RaycastHit" variable outside the if statement

    I haven't read all of your code, or checked if this work, but it should be something like this

    also try reading this or another example from the manual
    https://docs.unity3d.com/ScriptReference/RaycastHit-collider.html

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RaycastPickup : MonoBehaviour
    4. {
    5.  
    6.     float throwForce = 300;
    7.     RaycastHit hit;
    8.     GameObject item;
    9.     public GameObject tempParent;
    10.     bool isHolding = false;
    11.     float distance;
    12.     Vector3 objectPos;
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         RaycastHit hit;
    18.  
    19.         if (Input.GetMouseButton(0) && Physics.Raycast(transform.position, transform.forward, out hit, 4) && hit.transform.GetComponent<Rigidbody>())
    20.         {
    21.             item = hit.transform.gameObject;
    22.  
    23.             if (isHolding == true)
    24.             {
    25.                 isHolding = false;
    26.             }
    27.  
    28.  
    29.             else if (distance <= 4f)
    30.             {
    31.                 isHolding = true;
    32.                 item.GetComponent<Rigidbody>().useGravity = false;
    33.             }
    34.  
    35.         }
    36.  
    37.         distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
    38.         if (distance >= 4f)
    39.         {
    40.             isHolding = false;
    41.         }
    42.  
    43.         if (isHolding == true)
    44.         {
    45.             item.GetComponent<Rigidbody>().velocity = Vector3.zero;
    46.             item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    47.             item.transform.SetParent(tempParent.transform);
    48.  
    49.  
    50.             if (Input.GetMouseButtonDown(1))
    51.             {
    52.                 item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
    53.                 isHolding = false;
    54.             }
    55.         }
    56.         else
    57.         {
    58.             objectPos = item.transform.position;
    59.             item.transform.SetParent(null);
    60.             item.GetComponent<Rigidbody>().useGravity = true;
    61.             item.transform.position = objectPos;
    62.         }
    63.     }
    64. }
     
  5. SawyerK

    SawyerK

    Joined:
    Feb 11, 2016
    Posts:
    55
    Thank you! The distance thing works now but it gets me the same error for line 37 which is the distance, interesting.

    NullReferenceException: Object reference not set to an instance of an object
    RaycastPickup.Update () (at Assets/Scripts/RaycastPickup.cs:38)
     
  6. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    131
    are you setting "tempParent" somewhere?
     
  7. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    Line 21 in the code above does set the 'item' only if the condition is true.
    In line 37 you are using 'item' whether it was assigned, or not.
     
  8. SawyerK

    SawyerK

    Joined:
    Feb 11, 2016
    Posts:
    55
    I have an empty gameobject that is the guide that parented to the first person PlayerCamera. It float in front of the player at range 2.5.

    pickup1.jpg