Getting error message NullReferenceException: Object reference not set to an instance of an object GrappleGun.StartGrapple () (at Assets/GrappleGun.cs:38)
using UnityEngine;
public class GrappleGun : MonoBehaviour
{
public float grappleDistance = 100f;
public float grappleSpeed = 10f;
private LineRenderer lineRenderer;
private SpringJoint springJoint;
private bool isGrappling = false;
private void Start()
{
lineRenderer = GetComponent<LineRenderer>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && !isGrappling)
{
StartGrapple();
}
else if (Input.GetMouseButtonUp(0) && isGrappling)
{
StopGrapple();
}
if (isGrappling)
{
UpdateGrapplePosition();
}
}
private void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, grappleDistance))
{
isGrappling = true;
springJoint = gameObject.AddComponent<SpringJoint>();
springJoint.autoConfigureConnectedAnchor = false;
springJoint.connectedAnchor = hit.point;
springJoint.maxDistance = grappleDistance;
springJoint.spring = grappleSpeed;
springJoint.damper = 0.5f;
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, hit.point);
}
}
private void StopGrapple()
{
isGrappling = false;
Destroy(springJoint);
lineRenderer.positionCount = 0;
}
private void UpdateGrapplePosition()
{
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, springJoint.connectedAnchor);
}
}
Confused what I’m actually missing here.