0

The tutorial that I follow tells me to set a Transform to null, and it gives me an error

This is the code that I use

ObjectGrabbable script: (if I put this on a object then it can be grabbed)

type hereusing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectGrabbable : MonoBehaviour
{
    private Rigidbody objectRigidbody;
    private Transform objectGrabPointTransform;

    private void Awake()
    {
        objectRigidbody = GetComponent<Rigidbody>();
    }

    public void Grab(Transform objectGrabPointTransform)
    {
        this.objectGrabPointTransform = objectGrabPointTransform;
        Debug.Log("Da");
        objectRigidbody.useGravity = false;
    }

    public void Drop()
    {
        this.objectGrabPointTransform = null;
        objectRigidbody.useGravity = true;
    }

    private void FixedUpdate()
    {
        if(objectGrabPointTransform != null)
        {
            float lerpSpeed = 10f;
            Vector3 newPosition = Vector3.Lerp(transform.position, objectGrabPointTransform.position, Time.deltaTime * lerpSpeed);
            objectRigidbody.MovePosition(newPosition);
        }
    }
}

PlayerPickUpDrop script: (it does the picking up)

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

public class PlayerPickUpDrop : MonoBehaviour
{
    [SerializeField] private Transform playerCameraTransform;
    [SerializeField] private Transform objectGrabPointTransform;
    [SerializeField] private LayerMask pickUpLayerMask;

    private ObjectGrabbable objectGrabbable;

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.E))
        {   
            if(objectGrabbable == null)
            {
            float pickUpDistance = 2f;
            if(Physics.Raycast(playerCameraTransform.position, playerCameraTransform.forward, out RaycastHit raycastHit, pickUpDistance, pickUpLayerMask));
            {
                if(raycastHit.transform.TryGetComponent(out ObjectGrabbable objectGrabbable))
                   {
                    objectGrabbable.Grab(objectGrabPointTransform);
                    }
            } 
                
            }
            else 
            {
                objectGrabbable.Drop();
                Debug.Log("Nu");
            }
        }
    }
}

I want it to drop but it gives me a NullReferenceException: Object reference not set to an instance of an object PlayerPickUpDrop.Update () (at Assets/Scripts/PlayerPickUpDrop.cs:22) How can I fix this?

  • according to your error message the error is not in the objectGrabable script but in the PlayPickUpDrop script. looks like the raycastHit Object might be null, which would be null if you dont look at an object when you press E – Molbac Mar 15 '23 at 12:02
  • 1
    Read this [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it), find out which variable is null, assign it some value. – shingo Mar 15 '23 at 12:03
  • what exactly is line 22? Sounds like `objectGrabbable` is not assigned .. which makes sense since in `TryGetComponent(out ObjectGrabbable objectGrabbable)` you shadow the class field and create a new local variable `objectGrabbable` .. you probably want to remove the `ObjectGrabbable` type declaration there in order to actually assign your already exiting class field – derHugo Mar 15 '23 at 12:04
  • still wouldn't explain the exception though because it is actually wrapped in a null check ^^ – derHugo Mar 15 '23 at 12:06

0 Answers0