I'm having problems with the drag and drop script in my game?

I’m currently working on a mario maker style game and it’s got a bug that I don’t know how to fix, can anyone help?

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

public class DragDrop : MonoBehaviour
{
    public GameObject[] correctForm;

    public float startPosX, startPosY;
     public bool moving;
    public GameObject preFab;
    public GameObject Redsquare;

    private Vector3 resetPosition;
    private int ArrayValue = 0;
   
   
    /// <summary>
    /// Start is called on the frame when a script is enabled just before
    /// any of the Update methods is called the first time.
    /// </summary>
    void Start()
    {
        resetPosition = this.transform.localPosition;
    }
    /// <summary>
    /// Update is called every frame, if the MonoBehaviour is enabled.
    /// </summary>
    void Update()
    {
        if(moving)
        {
            Vector3 mousePos;
           mousePos = Input.mousePosition;
           mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
        }
    }

    public void RedSquare()
    {
        Instantiate(Redsquare);
    }

      
   
    private void OnMouseDown()
    {
        if(Input.GetMouseButtonDown(0))
        {
         Vector3 mousePos;
         mousePos = Input.mousePosition;
         mousePos = Camera.main.ScreenToWorldPoint(mousePos);

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

         moving = true;

        }
       
    }

    private void OnMouseUp()
    {  
        moving = false;
        if(Mathf.Abs(this.transform.localPosition.x - correctForm[ArrayValue].transform.localPosition.x) <= 0.5f &&
        Mathf.Abs(this.transform.localPosition.y - correctForm[ArrayValue].transform.localPosition.y) <= 0.5f)
        {
            this.transform.localPosition = new Vector3(correctForm[ArrayValue].transform.localPosition.x, correctForm[ArrayValue].transform.localPosition.y, correctForm[ArrayValue].transform.localPosition.z);
            Instantiate(preFab);
            ArrayValue ++;
          
        }
        else
        {
            this.transform.localPosition = new Vector3(resetPosition.x, resetPosition.y, resetPosition.z);
        }
    }
       
   
}

Going to need more info than “there’s a bug”.

Here’s a video
https://www.youtube.com/watch?v=Rs5pTwHPnRI

I´m not sure if the OnMouseDown and OnMouseUp events work for drag&drop. I think they are only for click event (but not sure about that).

Unity has some interfaces to hanlde drag&drop:

With that interfaces you have to implement three methods (OnDrag,OnEndDrag and OnDrop), with them you can handle drag&drop for your buttons.