I want to make an object that will go back to its original position after dragging it around. But when I release it, the object just went to the bottom left corner of the canvas. This is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class needle2 : MonoBehaviour, IPointerDownHandler, IEndDragHandler, IDragHandler
{
private Vector3 startPos;
public Button needles;
public GameObject needleonhand;
public Animator injection;
public bool playing = false;
void Start()
{
needleonhand.SetActive(false);
needles.gameObject.SetActive(true);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
gameObject.transform.position = startPos;
}
public void OnPointerDown(PointerEventData eventData)
{
gameObject.transform.position = startPos;
}
private void OnTriggerEnter2D(Collider2D OnNeedleTouch )
{
if(OnNeedleTouch.gameObject.name == "patientcure")
{
needleonhand.SetActive(true);
injection.Play("injection");
playing = true;
needles.gameObject.SetActive(false);
}
}
}