I have one canvas with three placeHolder and one Image over Canvas.
The Hierarchy is placeHolder’s and then Image.
I used EventTigger to put the Image with Drag and Drop over one similar placeHolder with tag “match”.
The Image is untagged.
This is the script I need to fix:
using UnityEngine;
using System.Collections;
public class ManageDragDrop : MonoBehaviour {
Vector3 initialPosition;
// Use this for initialization
void Start () {
initialPosition = gameObject.transform.position;
print (initialPosition);
}
// Update is called once per frame
void Update () {
}
public void Drag (){
//store position of mouse
GameObject.Find("Image").transform.position = Input.mousePosition;
}
public void Drop (){
for (int i=1;i<=3;i++){
GameObject placehold = GameObject.Find ("placeHolder"+i);
float distance = Vector3.Distance (GameObject.Find ("Image").transform.position, placehold.transform.position);
//print ("distance =" + distance);
if (distance < 50)
{
if (placehold.CompareTag("match")==true)
{
GameObject.Find ("Image").transform.position = placehold.transform.position;
print("match ok +"+initialPosition);
} else {
GameObject.Find ("Image").transform.position = initialPosition;
print("not mach +-"+initialPosition);
}
} else {
GameObject.Find ("Image").transform.position = initialPosition;
print("distance > 50 "+initialPosition);
}
}
}
}
The result is wrong. The image go to the left bottom screen.
One output is:
…
distance > 50 (0.0, 8.3, -4.1)
UnityEngine.MonoBehaviour:print(Object)
Why ? I don’t have -4.1 into my position of objects!!!?