After letting go of the mouse button, you can't pick up the object

Title is a little confusing. What I want is, after placing an object, you cannot drag it again after taking your finger off the mouse button. Right now, you can drag it again after taking your finger off.

The following code makes it so you can drag it each time when you click on it, idk what to add to it/modify it to make it so the object is no longer draggable after taking your finger off the mouse button:

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

public class ClickandDrag : MonoBehaviour
{
    GameObject road;

    private float startPosX;
    private float startPosY;
    private bool isBeingHeld = false;


   
    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);
    }                                                         




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

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

        if (Input.GetMouseButtonDown(0))
        {

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

            startPosX = mousePos.x - this.transform.localPosition.x; 
            startPosY = mousePos.y - this.transform.localPosition.y;

            isBeingHeld = true;
        }
    }
    private void OnMouseUp()
    {
        isBeingHeld = false;

    }

Add another variable; e.g. bool canBeHeld = true. Set it to false when the object is picked up, and then don’t allow the object to be picked up if it is false.

do I have to set it to false under each line where it needs to be? Right now, just set it to false under line 45 on line 46 and it didn’t work.

EDIT: I also put it on line 51 and that still didnt make it work.

Setting it to false under line 45 should be enough if you have correctly incorporated it into the condition on line 35 (and assuming nothing else is setting it to true).

Here’s what I have, it didnt work. And putting the bool at the end didnt worl either.

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

public class ClickandDrag : MonoBehaviour
{
    GameObject road;

    private float startPosX;
    private float startPosY;
    private bool isBeingHeld = false;

    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);
   




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

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

        if (Input.GetMouseButtonDown(0)) 
        {
            canBeHeld = true; //can still move it after being placed
            Vector3 mousePos; 
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            startPosX = mousePos.x - this.transform.localPosition.x; 
            startPosY = mousePos.y - this.transform.localPosition.y;

            isBeingHeld = true;
            canBeHeld = false; //can't move it after being placed
        }
    }
    private void OnMouseUp()
    {
        isBeingHeld = false;
      

    }

I don’t see any place in your code where you are testing whether the variable is true; you are only setting it to true, then setting it to false.

Variables don’t do anything on their own, they’re just a way for your program to remember stuff. You also have to tell the program what you want it to do based on the stuff it remembers.

The simplest way to do that is to say

if (something is the case) then (do this thing)

Your OnMouseDown function already has a condition that says “only do this if mouse button #0 is down”. You need to modify it to also say “only do this if canBeHeld is currently true”.

Got it working. Thanks.

I also had to use a previous project to see what the format was.

under the “get mouse down” if statement, I had to include another if statement but that statement only included this: if(canBeHeld)

after I included that, “canBeHeld=false” worked.