The touch is stuck in the middle of the screen. drag and shoot movement

I went through a drag and shoot guide to the phone game I make,
The Touch looks like it’s stuck in the middle of the screen. When I tried to click on the screen I got an error saying that I have problems on lines 39, 47, 57 and all of these lines have the code:
vector3 “name” = cam.ScreenToWorldPoint (touch.position);

Here is the script

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

public class PlayerControll : MonoBehaviour
{
    public float power = 10f;
    public float maxdrag = 5f;
    public Rigidbody2D rb;
    public LineRenderer lr;
    public Camera cam;

    Vector3 dragStartPos;
    Touch touch;

    private void Update()
    {
        if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);


            if (touch.phase == TouchPhase.Began) {
                DragStart();
            }
            if (touch.phase == TouchPhase.Moved) {
                Dragging();
            }
            if (touch.phase == TouchPhase.Ended) {
                DragRelease();
            }
        }


    }

    void DragStart()
    {
        dragStartPos = cam.ScreenToWorldPoint(touch.position);
        dragStartPos.z = 0f;
        lr.positionCount = 1;
        lr.SetPosition(0, dragStartPos);
    }

    void Dragging()
    {
    Vector3 dragginPos = cam.ScreenToWorldPoint(touch.position);
        dragginPos.z = 0f;
        lr.positionCount = 2;
        lr.SetPosition(1, dragginPos);
    }

    void DragRelease()
    {
        lr.positionCount = 0;

        Vector3 dragReleasePos = cam.ScreenToWorldPoint(touch.position);
        dragReleasePos.z = 0f;

        Vector3 force = dragStartPos - dragReleasePos;
        Vector3 clampedForce = Vector3.ClampMagnitude(force, maxdrag) * power;

        rb.AddForce(clampedForce, ForceMode2D.Impulse);
    }
}

What am I doing wrong?

What does your error say? :stuck_out_tongue:

I fixed the error by turning the camera into the main camera but the problem still exists!

Does the line renderer work? If not, what do you get when you put Debug.Log(dragginPos) in Dragging()?

My other worry is that setting .positionCount constantly might be resetting the vertices. Maybe only do it on DragStart():

void DragStart()
    {
        dragStartPos = cam.ScreenToWorldPoint(touch.position);
        dragStartPos.z = 0f;
        lr.positionCount = 2;
        lr.SetPosition(0, dragStartPos);
        lr.SetPosition(1, dragStartPos);
    }

If you have a problem with this as well try doing instead of dragStartPos = cam.ScreenToWorldPoint(touch.position);
do Camera.main.ScreenToWorldPoint(touch.position); or possibly Camera.ScreenToWorldPoint(touch.position);

Dont work