I have a prefab and I have put a few of them on the screen. I am not instantiating these at runtime I am instantiating them at design. I have created a script to make them move based on the mouse location once clicked on. For some reason when I click on one of the objects, the isGrabbed variable becomes true for all of them and they all move to the same location on top of each other. Can’t seem to find the error here. Can anyone help out?
using UnityEngine;
using System.Collections;
public class TestMove : MonoBehaviour {
public Vector3 mousePos = new Vector3(0,0,0);
public RaycastHit hit;
public bool isGrabbed;
public Vector3 newLocation = new Vector3(0,0,0);
public Vector3 initialPos = new Vector3(0,0,0);
public Ray ray;
public string name;
// Use this for initialization
void Start () {
transform.position =initialPos;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
mousePos = Camera.main.ScreenToWorldPoint( Input.mousePosition);
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
newLocation.x = mousePos.x;
newLocation.y = mousePos.y;
if (Physics.Raycast(ray, out hit))
{
isGrabbed = true;
transform.position = newLocation;
}
}
else
{
isGrabbed = false;
}
if (isGrabbed){
}
Debug.Log (name + transform.position);
}
}