I'm making a 2D puzzle game on android. My code was working fine when I was only working with one object. But then I added more objects and now when I touch one of the puzzle pieces , I'm dragging all of the puzzle pieces on the screen. By the way, this script is attached to all pieces. Here's my code :
public Transform right_particle;
public Transform wrong_particle;
public string pieceStatus = "";
void Update()
{
if (pieceStatus != "locked" && Input.GetMouseButton (0)) {
float distance_to_screen = Camera.main.WorldToScreenPoint (gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
gameObject.transform.position = new Vector3 (pos_move.x, pos_move.y, pos_move.z);
}}
void OnTriggerStay2D(Collider2D other)
{
if ((other.gameObject.name == gameObject.name) && Input.GetMouseButtonUp(0) ){
other.GetComponent<BoxCollider2D> ().enabled = false;
GetComponent<BoxCollider2D> ().enabled = false;
transform.position = other.gameObject.transform.position;
pieceStatus = "locked";
Instantiate (right_particle, other.gameObject.transform.position, right_particle.rotation);
}
else if ((other.gameObject.name != gameObject.name) && Input.GetMouseButtonUp (0)) {
Instantiate (wrong_particle, other.gameObject.transform.position, right_particle.rotation);
}
}
I've also tried with raycasting but none of the pieces moved. I also have one more problem with the OnTriggerStay2D, "else if" part. OnTriggerStay2D should instantiate one of the particles according to puzzle piece is in the correct place or not. But it instantiates both of them when piece is in the correct place. I can use any help especially with the first question. Thank you everyone.