move object without having to hold down mouse button

I would like to be able to left click on an object and move it without having to hold down the left mouse button, and then place the object when I click the button again.

The code below makes it so you have to hold the mouse button to move the object, and the object is placed when letting go of the mouse button. What do I need to change to get it so I no longer have to hold down the button and place it when clicking the button again?

The “road” gameObject will be replaced with a building, that was just already done so just wanted to test the code.

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

public class ClickandDrag : MonoBehaviour

//to place buildings
{
    GameObject road;

    private float startPosX;
    private float startPosY;
    private bool isBeingHeld = false;  //check to see if object is being clicked by mouse

    private bool canBeHeld = true;




    void Update()
    {

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

        if (isBeingHeld == true)
            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0); //just put a 0 for the "z"
    }                                                             //these-->mousePos.x - startPosX and mousePos.y - startPosY also had to been added in this line to also make the object not move when clicking in a spot




    //-----------------------------------------------------------

    //this section was all about getting the mouse position and setting the variable
    private void OnMouseDown()
    {

        if (Input.GetMouseButtonDown(0)) //"0" is left button, "1" is right button
        {


            if (canBeHeld)//this sets it up so later on in the code when its placed, it cant be moved
            {


                Vector3 mousePos;  //xyz of mouse position on scree of the whole computer
                mousePos = Input.mousePosition;
                mousePos = Camera.main.ScreenToWorldPoint(mousePos); //converting screen point of the mouse to the world point (the ingame point)

                startPosX = mousePos.x - this.transform.localPosition.x;  //these two lines makes it so the object doesnt move from clicking in a certain spot
                startPosY = mousePos.y - this.transform.localPosition.y;

                isBeingHeld = true;




                canBeHeld = false; //"false" means we can't move it after being placed
            }
        }
    }
    private void OnMouseUp()
    {
        isBeingHeld = false; //when we let go of the mouse button, its saying we are letting go


        {


        }


    }
}
if (Input.GetMouseButtonDown(0))
{
   pressed = true;
}
if (pressed && Input.GetMouseButtonUp(0))
{
   canBeHeld = true;
}
if (canBeHeld)//this sets it up so later on in the code when its placed, it cant be moved
{
   //.. your code
}
//but you need to reset "pressed" value somewhere.
//Maybe on right click just call:
pressed = false;

Thanks! it was really horrible having to hold down the left click the entire time lol.

And I learned something new about coding too. Knowing that our variable name can be anything, for the last two or three years, I was wondering how the heck does Unity know what our variable is used for? Like here, how does it know “canbeheld” and “pressed” with how we want to use it, well I finally put two and two together and variables correspond to the function “OnMouseDown”. So, we make “OnMouseDown” function based on our code from the variables.

I tried so many different things and the object is not being placed when pressing left click again or pressing right click

Maybe you should call on right click

isBeingHeld = false

Also if you want to cancel on another left click just do:

if (Input.GetMouseButtonDown(0))
{
  if (pressed)
  {
    isBeingHeld = false;
  }
  pressed = !pressed;
}

Idk, I cant seem to get it to work, here’s my attempt (starts on line 69):

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

public class ClickandDrag : MonoBehaviour

//to place buildings
//you can press button once, or hold it down to move it to where you want to place it
//once placed, you cant move it
//this script can be attached to any building
{
    GameObject road;

    private float startPosX;
    private float startPosY;
    private bool isBeingHeld = false;  //check to see if object is being clicked by mouse

    private bool canBeHeld = true;
    private bool pressed = true;




    void Update()
    {

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

        if (isBeingHeld == true)
            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0); //just put a 0 for the "z"
    }                                                             //these-->mousePos.x - startPosX and mousePos.y - startPosY also had to been added in this line to also make the object not move when clicking in a spot




    //-----------------------------------------------------------

    //this section was all about getting the mouse position and setting the variable
    private void OnMouseDown()
    {

        if (Input.GetMouseButtonDown(0)) //"0" is left button, "1" is right button
        {
            pressed = true;
        }
        if (pressed && Input.GetMouseButtonUp(0))//this sets it up so later on in the code when its placed, it cant be moved
            canBeHeld = true;
        {


            Vector3 mousePos;  //xyz of mouse position on scree of the whole computer
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos); //converting screen point of the mouse to the world point (the ingame point)

            startPosX = mousePos.x - this.transform.localPosition.x;  //these two lines makes it so the object doesnt move from clicking in a certain spot
            startPosY = mousePos.y - this.transform.localPosition.y;

            isBeingHeld = true;




            pressed = false; //"false" means we can't move it after being placed
        }

        //the code below tried to get it so when clicking left again, the object is placed
        if (Input.GetMouseButtonDown(0))


        {
            {
                if (pressed)

                {
                    isBeingHeld = false;
                }
                pressed = !pressed;
            }
        }



        pressed = false; //when we let go of the mouse button, its saying we are letting go

    }
}

Line 85 always executes.

I tried to get rid of it, and that didnt work. So i know line 85 wasn’t the problem. I tried a couple of other things which didnt work either. Clicking the object and moving it without having to hold down the button works, but clicking the button again doesnt do anything, it doesnt place the object so you are forever grabbing/moving it with your mouse