DestroyObject when its code executed twice

Hello, I’m here to ask for an solution to a problem that i encountered in my drag and drop code.

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

public class EventArgs{}

public class Draggable : MonoBehaviour
{
    Vector3 mousePositionOffset;
    
    private Vector3 GetMouseWorldPosition()
    {
        return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    
    private void OnMouseDown()
    {
        mousePositionOffset = gameObject.transform.position - GetMouseWorldPosition();
    }
    
    private void OnMouseDrag()
    {
        transform.position = GetMouseWorldPosition() + mousePositionOffset;
        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = true;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = true;
    }
    
    private void OnMouseExit()
    {
        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = false;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = false;
    }
}

I would really love if this code would include that the object, after being moved twice, would then get destroyed.

The Solution Highly Appreciated.

To destroy the object after it has been moved twice, you can introduce a counter variable and check it in your OnMouseDrag() method.

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

public class Draggable : MonoBehaviour
{
    Vector3 mousePositionOffset;
    int moveCount = 0; // Counter to keep track of moves.

    private Vector3 GetMouseWorldPosition()
    {
        return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    private void OnMouseDown()
    {
        mousePositionOffset = gameObject.transform.position - GetMouseWorldPosition();
    }

    private void OnMouseDrag()
    {
        transform.position = GetMouseWorldPosition() + mousePositionOffset;
        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = true;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = true;
    }

    private void OnMouseUp()
    {
        moveCount++;

        if (moveCount >= 2)
        {
            // Destroy the object after being moved twice.
            Destroy(gameObject);
        }

        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = false;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = false;
    }
}

Hi, Lyrintacy

Just simply add a counter to your object and increase it when the player stops to grab it. And destroy the game object when the counter reaches two.

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

public class EventArgs { }
public class Draggable : MonoBehaviour
{
    Vector3 mousePositionOffset;
    public int Moved;
    private Vector3 GetMouseWorldPosition()
    {
        return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    private void OnMouseDown()
    {
        mousePositionOffset = gameObject.transform.position - GetMouseWorldPosition();
    }

    private void OnMouseDrag()
    {
        transform.position = GetMouseWorldPosition() + mousePositionOffset;
        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = true;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = true;
    }

    private void OnMouseExit()
    {
        (gameObject.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D).isTrigger = false;
        (gameObject.GetComponent(typeof(EdgeCollider2D)) as EdgeCollider2D).isTrigger = false;
        Moved++;
    }
    private void Update()
    {
        if (Moved >= 2)
        {
            Destroy(gameObject);
        }
    }
}