NullReferenceException while Creating Drag and Shoot Script

This is for my first project with Unity, thats why i copied the following scripts from a youtubevideo(

). I checked multiple times for mistakes i made, but everything is just like in the Tutorial.

These are the errors i’m getting:
NullReferenceException: Object reference not set to an instance of an object
Drag.Update () (at Assets/Drag.cs:53)

NullReferenceException: Object reference not set to an instance of an object
Drag.Update () (at Assets/Drag.cs:41)

No TrajectoryLine is appearing but the Drag and Shoot mechanism is working.

  1. Script = TrajectoryLine(Definition of LineRenderer)
  2. Script = DragAndShoot(Referencing 1.Script)

I think the problem has to do with not referncing the 1.Script correctly.
How do i fix this?

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

[RequireComponent(typeof(LineRenderer))]

public class TrajectoryLine : MonoBehaviour
{
    public LineRenderer lr;

    private void Awake()
    {
        lr = GetComponent<LineRenderer>();
    }

    public void RenderLine(Vector3 startPoint, Vector3 endPoint)
    {
        lr.positionCount = 2;
        Vector3[] points = new Vector3[2];
        points[0] = startPoint;
        points[1] = endPoint;

        lr.SetPositions(points);
    }

    public void EndLine ()
    {
        lr.positionCount = 0;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drag : MonoBehaviour
{
    public float power = 10f;
    public Rigidbody2D rb;

    public Vector2 minPower;
    public Vector2 maxPower;

    TrajectoryLine tl;
 
    Camera cam;
    Vector2 force;
    Vector3 startPoint;
    Vector3 endPoint;

    private void Start()
    {
        cam = Camera.main;
   
        tl = GetComponent<TrajectoryLine>();
    }


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            startPoint.z = 15;
 
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            currentPoint.z = 15;
            tl.RenderLine(startPoint, currentPoint);
     
        }   
   
   
        if (Input.GetMouseButtonUp(0))
        {
            endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            endPoint.z = 15;
       
            force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
            rb.AddForce(force * power, ForceMode2D.Impulse);
            tl.EndLine();

        }
    }
}

Not sure on the line 41 error as that one makes no sense to me, but the other could be that you just forgot to set rb. I don’t see you setting it anywhere in the script so you’re relying on setting it in the editor and if you forgot to do that it would complain about it being not set.

  • Did you assign the Rigidbody2D reference in the Drag script’s inspector?
  • Does the GameObject that the Drag script is attached to also have a TrajectoryLine component?