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?