Scripting affecting all clones.

Hello,

I have a game where I have 10 game objects that are all the same. All have the same script and define d sections. I have a file called userInput that allows the user to click on the enemy and throw them. Everything works perfect when I only have one skeleton on the . When all 10 are on the board and try to move one game objet it moves all of them and i cant figure out why. any help would be nice.

using UnityEngine;
using System.Collections;

public class UserInput : MonoBehaviour {

    public EnemyMovment enemyMovment;
    public EnemyHealth enemyHealthFile; 
    const float LAUNCH_SPEED = 3000;
    //store gameObject reference
    public Object BottomWall;
    GameObject enemyBottomWall;
    public Transform castle;
    public GameObject Enemy_Obj;
    public Rigidbody2D rigidbody;
    public float movesSpeed = 10;
    //public Camera camera;

    bool mouseButtonReleased;

    //holds mouse position
    Vector3 mouseLastPosition;
    Vector3 mouseCurrentPosition;
    Vector3 mouseDiffrenceInPosition;
    //Vector3 skeletonXPosition;

    int counter;
    public Vector2 enemyHighestYPosition;
    Vector2 mMouseDownPos;
    Vector2 mMouseUpPos;
    public float speed;
    public float throwLeftOrRight;  // positive throws right and left throws blank
    public float throwUpOrDown;     // positive throws up and negative throws down
    public Transform skeleton;              //castle target
    public SpriteRenderer spriteRenderer; //sprite render refrence
    private Animator animator;            //animator refrence
    public Sprite Idle;

    public Vector2 yLimit;

    public float impactForce;




    // Use this for initialization
    void Start ()
    {

        //gets animator for animator refrence
        animator = GetComponent<Animator>();
        rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update ()
    {

   

        //statment that will save old position every other time to increase distances for projection calculations
        if (counter % 2 == 0)
        {
            //Saves last mouse poition
            mouseLastPosition = mouseCurrentPosition;

        }


        //saves the enemys highest y position if it is higher then before
        if (enemyHighestYPosition.y < transform.position.y)
        {
           
            enemyHighestYPosition.y = transform.position.y;

        }


        //Gets new mouse position
        mouseCurrentPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10);

        //increments counter for if statment
        counter++;

        //function that checks when the mouse button is released. It will performe the actions below when the mouse is released
        if (Input.GetMouseButtonUp (0))
        {

            //Ingnores collision between wall layer and building layer
            //Allows enemy to pass threw buildings when picked up
            //Physics2D.IgnoreLayerCollision(8, 9,true);
       
            //Calculates the diffrence in position between last mouse position and current
            mouseDiffrenceInPosition = mouseCurrentPosition - mouseLastPosition;

            //calculates which way to throw character based on mouse position diffrence * launch speed
            throwLeftOrRight = (mouseDiffrenceInPosition.x / 2)* LAUNCH_SPEED;
            throwUpOrDown = (mouseDiffrenceInPosition.y / 2) * LAUNCH_SPEED;

            //Throws enemy in direction calculated by mouse position
            rigidbody.AddForce(new Vector2(throwLeftOrRight, throwUpOrDown));

            //adds gravity to enemy
            rigidbody.gravityScale = 16;


            //marks the the mouse button was released
            mouseButtonReleased = true;
        }



        if (transform.position.y < yLimit.y && mouseButtonReleased == true)
            {


                //Stops force that was being applied to the enemy x position
                rigidbody.velocity = Vector3.zero;

                //Stops force that was being applied to the enemy y position and casuses them to stop at the same spot
                //transform.position = new Vector2(transform.position.x, yLimit.y);

                //turns off gravity
                rigidbody.gravityScale = 0;
                animator.speed = 1f;
                enemyMovment.enemyWalkingAction = true;   //makes player start walking

                //starts animation
                yLimit.y = -10000;



                //calculates the fall damage
                impactForce = enemyHighestYPosition.y - transform.position.y;

                //applys the fall damage to the enemy
                enemyHealthFile.EnemyTakeDamage(impactForce);

                Debug.Log ("The impact force is: " + impactForce);

                //Resets all variables for next throw
                mouseButtonReleased = false;
                enemyHighestYPosition.y = 0;

            }
   
                }

    //function that detects if the enemy has the mouse over them
    public void OnMouseOver(){

        //If statment that detects if the enemy has been clicked on
        if (Input.GetMouseButtonDown(0)) {


            //Enemy_Obj.layer = 8;               //sets enemy layer to wall layer
            //spriteRenderer.sprite = Idle;      //changes sprite to idle sprite
            //animator.Stop(); 
            animator.speed = 0f;
            //animator.Stop();                   //stops animation


            //makes player stop walking*/

            //Gets the x position of the enemy
            //skeletonXPosition = transform.position;
            //sets the y and z to zero
            //skeletonXPosition.y = 0;
            //skeletonXPosition.z = 0;

            yLimit.y = transform.position.y;

        }

    }

    //drags the enemy with the mouse
    public void OnMouseDrag()
    {
       
        //moves skeleton to mouse position
        Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10);
        Vector3 objectPosition = Camera.main.ScreenToWorldPoint (mousePosition);

        transform.position = objectPosition;
    }
}

How does your game check which enemy is clicked on? What is stopping all of your scripts acting upon the OnMouseDrag event?

What it looks like to me is that your game will act on the OnMouseDrag irrespective of which enemy you clicked on. So it just takes all of that parameters and perform the action. What you might want to do instead is handle the click and drag separately and pass the input information directly to the correct gameobject’s script. Then let that script handle the outcome of whatever the input information was.

For example you might handle the click and drag to get the Vector3s, then pass both Vector3’s to that single object.