How to get the object to move only when I click on it and not just anywhere else?
Hi everyone i was trying to make a 2D game where mt gameobject(key) moves when I click on it.Well I was able to move it using the c# script below but, the problem is that when I add more than one object and when i click they all move at once. I just want them to move only I click on them and not just anywhere else on the screen not even on any other object. Please note that I am not using Transform since I am dealing with collisions and I don't my object to shake on collision . ANY HELP IS APPRECIATED. THE SCRIPT-- using UnityEngine; using System.Collections;
 public class movement2 : MonoBehaviour {
 
     void Start () {
     
     }
 
     void Update () {
         if (Input.GetMouseButton(0)) {
             GetComponent<Rigidbody2D>().AddForce (new Vector2(-3,0));
         }
     
     }
 }
 
              Answer by Oribow · Oct 07, 2015 at 02:14 PM
If you place the script on each GameObject you want to drag, you can use OnMouseOver() it's getting called on the GameObject, when the mouse is over it.
THAN$$anonymous$$ YOU solved half of my problem. I have the same problem in this case but here my object will move on click only when it detects collision with the object"LOC$$anonymous$$" . Any idea how I can to do it in this case?
 using UnityEngine;
 using System.Collections;
 
 public class collision2 : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
 
     void OnCollisionStay2D(Collision2D coll)    
     {
         if (coll.gameObject.tag == "LOC$$anonymous$$") {
             if (Input.Get$$anonymous$$ouseButton (0)) {
                 GetComponent<Rigidbody2D> ().AddForce (new Vector2 (18, 0));
             }
         }
     }
 
 }
 
                 Your second script will only react if the GameObject it is on collide with a GameObject that has the tag == "LOC$$anonymous$$". But I dont understand what you want. Could you explain more how you want the script to behaive?
So I will explain it with this picture. If you need any more information please ask me, and thank you for helping me out.
Your answer