Move object with mouse

I don’t see why my code won’t work

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

public class movepice : MonoBehaviour
{
	private bool selected;
	// Start is called before the first frame update
	void Start()
	{

	}


	void OnMouseOver()
	{
		if(Input.GetMouseButtonDown(0))
		{
			selected = true;
		}
			
	}

	// Update is called once per frame
	void Update()
	{
		if(selected == true)
		{
			Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			transform.position = new Vector2(cursorPos.x, cursorPos.y);
		}
		if(Input.GetMouseButtonUp(0))
		{
			selected = false;
		}
	}
}

please help

You’re calling for two contradictory if statements in the update function at the same time. One requires “selected” to be true whereas right at that moment, the other makes “selected” false. Simply remove the second if statement and within the first one add in your line selected = false;
@puckloe