null reference exception on a valid value

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)
"

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MousePos2D : MonoBehaviour
{
    [SerializeField] private Camera mCam;
    public GameObject Curser;



    private void Update()
    {
        Ray ray = mCam.ScreenPointToRay(Input.mousePosition);
        RaycastHit raydata = new();
        transform.position = raydata.transform.position;
        float rayx = ray.origin.x;
        float rayy = ray.origin.y - 1.25f;
        float rayz = ray.origin.z;
        Vector3 Raypos = new Vector3(rayx, rayy, rayz);
        transform.position = Raypos;

        if (Input.GetMouseButton(0))
        {
            // this does "raycastHit.transform. = raycastHit.collider.gameObject.transform;" in a jank way
            raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = true;
            raydata.collider.transform.SetParent(Curser.transform);
        }
        else IfNotDown();

        void IfNotDown()
        {
            // Input.GetMouseButtonUp doesnt work...
            raydata.collider.transform.parent = null;
            raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
        }
    }
}

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:

        else IfNotDown();
        void IfNotDown()
        {
            // Input.GetMouseButtonUp doesnt work...
            raydata.collider.transform.parent = null;
            raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
        }

is very strange. You could just do this:

        else
        {
            // Input.GetMouseButtonUp doesnt work...
            raydata.collider.transform.parent = null;
            raydata.collider.transform.GetComponent<Rigidbody>().isKinematic = false;
        }

The problem with that is this code:

        float rayx = ray.origin.x;
        float rayy = ray.origin.y - 1.25f;
        float rayz = ray.origin.z;
        Vector3 Raypos = new Vector3(rayx, rayy, rayz);
        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