I am making a game which involves a lot of drag and drop. I have some objects that the player has to drag into the object slot. How do I destroy the object if the player drags it outside the slot or somewhere randomly on the canvas. This is the code I have used.
`
public class dragDrop : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
Image image;
CanvasGroup canvasGroup;
Vector2 initPosition;
void Awake(){
Time.timeScale = 1;
image = GetComponent<Image>();
canvasGroup = GetComponent<CanvasGroup>();
initPosition = transform.position;
}
public void OnBeginDrag(PointerEventData eventData)
{
canvasGroup.alpha = 0.5f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
}
`