make object only animate when button held(2D)

Here is my code:
using UnityEngine;
using System.Collections;

public class MouseMove2D : MonoBehaviour {
 
    private Vector3 mousePosition;
    public float moveSpeed = 0.1f;
 
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton(1)) {
            mousePosition = Input.mousePosition;
            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
        }
 
    }
}

It does not work, and gives me error codes. There is pry an easy fix i am too dumb to see. Any help is appreciated!,here is my code:

using UnityEngine;
using System.Collections;
 
public class MouseMove2D : MonoBehaviour {
 
    private Vector3 mousePosition;
    public float moveSpeed = 0.1f;
 
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton(1)) {
            mousePosition = Input.mousePosition;
            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
        }
 
    }
}

it gives me error codes, and does not work. It probably has an easy fix that i am too dumb to see. Any help is appreciated!

I believe that the problem is your mousePosition is assigned to a function using itself → mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

You could try replacing

mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

with just

    mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Or add a different variable to the Input.mousePosition

inputMouse = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(inputMouse);

Hope this helps