Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback null reference exception on a valid value

Discussion in 'Editor & General Support' started by oc1345, Apr 26, 2023.

  1. oc1345

    oc1345

    Joined:
    Mar 27, 2023
    Posts:
    4
    I'm new to Unity, C# and asking for support. i tried to make it so that if the mouse button is pushed it picks up the object and if mouse button is not pushed it drops the object but instead it puts out error
    "
    NullReferenceException: Object reference not set to an instance of an object
    MousePos2D.Update () (at Assets/Scripts/MousePos2D.cs:17)
    "

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MousePos2D : MonoBehaviour
    7. {
    8.     [SerializeField] private Camera mCam;
    9.     public GameObject Curser;
    10.  
    11.  
    12.  
    13.     private void Update()
    14.     {
    15.         Ray ray = mCam.ScreenPointToRay(Input.mousePosition);
    16.         RaycastHit raydata = new();
    17.         transform.position = raydata.transform.position;
    18.         float rayx = ray.origin.x;
    19.         float rayy = ray.origin.y - 1.25f;
    20.         float rayz = ray.origin.z;
    21.         Vector3 Raypos = new Vector3(rayx, rayy, rayz);
    22.         transform.position = Raypos;
    23.  
    24.         if (Input.GetMouseButton(0))
    25.         {
    26.             // this does "raycastHit.transform. = raycastHit.collider.gameObject.transform;" in a jank way
    27.             raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = true;
    28.             raydata.collider.transform.SetParent(Curser.transform);
    29.         }
    30.         else IfNotDown();
    31.  
    32.         void IfNotDown()
    33.         {
    34.             // Input.GetMouseButtonUp doesnt work...
    35.             raydata.collider.transform.parent = null;
    36.             raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
    37.         }
    38.     }
    39. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    You're not actually doing a raycast at all. You're just creating a new empty RaycastHit object and then trying to access data in it. But there is no data in it, because you never actually performed a raycast.
    raydata.transform
    is null, and trying to access the position of null is why you are getting your error.

    The solution is to actually perform a raycast and only if the raycast hits something then you can run your code. You also have some really weird constructs in your code. For example:

    Code (CSharp):
    1.         else IfNotDown();
    2.         void IfNotDown()
    3.         {
    4.             // Input.GetMouseButtonUp doesnt work...
    5.             raydata.collider.transform.parent = null;
    6.             raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
    7.         }
    is very strange. You could just do this:

    Code (CSharp):
    1.         else
    2.         {
    3.             // Input.GetMouseButtonUp doesnt work...
    4.             raydata.collider.transform.parent = null;
    5.             raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
    6.         }
     
  3. oc1345

    oc1345

    Joined:
    Mar 27, 2023
    Posts:
    4
    The problem with that is this code:
    Code (CSharp):
    1.         float rayx = ray.origin.x;
    2.         float rayy = ray.origin.y - 1.25f;
    3.         float rayz = ray.origin.z;
    4.         Vector3 Raypos = new Vector3(rayx, rayy, rayz);
    5.         transform.position = Raypos;
    Due to this the object will be a bit lower than the cursor so the object is not in the camera

    so
    if (Input.GetMouseButton(0))
    in
    physics.raycast
    will not work unless the raycast hit an object which it wont do unless the person lowers the cursor.

    Thanks for fixing my weird constructs