Issues with dropping an object on multiple objects separetly - Simple 2d Drag & Drop
Hello,
What I want to achieve is: Drag and Drop the circle in the Start Screenshot on the squares separetly, so you'd drag and drop it on any square, and that square will be destroied, then continue to the other square after the circle is reset to the initial position.
The issue: The issue I have is that I have to drag the circle on the squares in order of the newest square created, so when I drag and drop on sq3>sq2>sq1 "sq3 is the last one created, and it's on the far right, sq2 is in the middle, sq1 is on the left", it doesn't show any errors, but when I drag and drop on sq2>sq1 then try to drag and drop on sq3, it shows an error of "Object reference not set to an instance of an object" as in the error screenshot, I've tried to findbygameobjecttag, but it also does the same thing, so I though the find alone will look for the name, since the find by tag looks for sorting order as well.
My Questions:
1- Is there a way I can make it disregard the order of the last created stack/list, where I can drag and drop the circle to any of the squares without any order?
2- Is there a way to attach the destroy code to the squares instead of having it in the circle code?
I would love if I can detect the drop of the circle from the square code, I know the OnCollisionEnter function, it works really good, but it detects the collision, but instead, I would love to detect the drop of another object on it, something like the OnMouseUp.
Below is the code I have attached to the circle, and it is the only code I'm using.
private Vector2 initialPosition;
private Vector2 mousePosition;
private float deltaX, deltaY;
void Start()
{
initialPosition = transform.position;
}
private void OnMouseDown()
{
deltaX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
deltaY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
private void OnMouseDrag()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(mousePosition.x - deltaX, mousePosition.y - deltaY);
}
private void OnMouseUp()
{
if (Mathf.Abs(transform.position.x - GameObject.Find("sq1").transform.position.x) <= 3f &&
Mathf.Abs(transform.position.y - GameObject.Find("sq1").transform.position.y) <= 3f)
{
Destroy(GameObject.Find("sq1"));
transform.position = initialPosition;
}
else if (Mathf.Abs(transform.position.x - GameObject.Find("sq2").transform.position.x) <= 3f &&
Mathf.Abs(transform.position.y - GameObject.Find("sq2").transform.position.y) <= 3f)
{
Destroy(GameObject.Find("sq2"));
transform.position = initialPosition;
}
else if (Mathf.Abs(transform.position.x - GameObject.Find("sq3").transform.position.x) <= 3f &&
Mathf.Abs(transform.position.y - GameObject.Find("sq3").transform.position.y) <= 3f)
{
Destroy(GameObject.Find("sq3"));
transform.position = initialPosition;
}
else
{
Debug.Log("Error");
}
}
Please note that I'm still a begginer, and I don't know a lot in C# coding, specially raycasting and all that stuff, so please if you recommend using any other type of code than the one above, please provide me with some information and examples if possible.
Many thanks!!!