Clicking again does not place the object

Left clicking once “grabs” the object and you can freely move it without needing to hold down the left click button. That part was intended and works fine. What doesn’t work is placing the object when hitting left click again. its forever grabbing it.

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

public class ClickandDrag : MonoBehaviour


{
    public GameObject testobject;

    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

    }
}

Your OnMouseDown is certainly the source of your problem. You need to walk through the logic to see what is triggering. All those if statements…chances are you actually want some if else statements

As in the previous reply, your if logic is suspect. This doesn’t look like correct syntax/logic. What is the second { character for? Put debug.log statements before each If statement to follow the logic Tips for new Unity users

      if (Input.GetMouseButtonDown(0)) //"0" is left button, "1" is right button
        {
            pressed = true;
        }
        { if