Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in 'Getting Started' started by peemuee, Jun 23, 2023.

  1. peemuee

    peemuee

    Joined:
    Jun 23, 2023
    Posts:
    1
    Getting error message NullReferenceException: Object reference not set to an instance of an object GrappleGun.StartGrapple () (at Assets/GrappleGun.cs:38)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GrappleGun : MonoBehaviour
    4. {
    5.     public float grappleDistance = 100f;
    6.     public float grappleSpeed = 10f;
    7.  
    8.     private LineRenderer lineRenderer;
    9.     private SpringJoint springJoint;
    10.     private bool isGrappling = false;
    11.  
    12.     private void Start()
    13.     {
    14.         lineRenderer = GetComponent<LineRenderer>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0) && !isGrappling)
    20.         {
    21.             StartGrapple();
    22.         }
    23.         else if (Input.GetMouseButtonUp(0) && isGrappling)
    24.         {
    25.             StopGrapple();
    26.         }
    27.  
    28.         if (isGrappling)
    29.         {
    30.             UpdateGrapplePosition();
    31.         }
    32.     }
    33.  
    34.     private void StartGrapple()
    35.     {
    36.         RaycastHit hit;
    37.  
    38.         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, grappleDistance))
    39.         {
    40.             isGrappling = true;
    41.  
    42.             springJoint = gameObject.AddComponent<SpringJoint>();
    43.             springJoint.autoConfigureConnectedAnchor = false;
    44.             springJoint.connectedAnchor = hit.point;
    45.             springJoint.maxDistance = grappleDistance;
    46.             springJoint.spring = grappleSpeed;
    47.             springJoint.damper = 0.5f;
    48.  
    49.             lineRenderer.positionCount = 2;
    50.             lineRenderer.SetPosition(0, transform.position);
    51.             lineRenderer.SetPosition(1, hit.point);
    52.         }
    53.     }
    54.  
    55.     private void StopGrapple()
    56.     {
    57.         isGrappling = false;
    58.         Destroy(springJoint);
    59.  
    60.         lineRenderer.positionCount = 0;
    61.     }
    62.  
    63.     private void UpdateGrapplePosition()
    64.     {
    65.         lineRenderer.SetPosition(0, transform.position);
    66.         lineRenderer.SetPosition(1, springJoint.connectedAnchor);
    67.     }
    68. }
    Confused what I'm actually missing here.
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Before line 38 add:
    Code (CSharp):
    1. Debug.Log($"Camera is null: {Camera.main == null}");
    If you get true you don't have Main Camera active. The main camera is that camera that has the tag "MainCamera" (not the name of the game object) and such game object must be active.
     
    Last edited: Jun 23, 2023