- Home /
 
How to move the other object without moving the first object?
Hi guys, I have 2 object (both with Player tag as well) both with navmesh and the same PlayerMovement script attach to them and I have a empty game object with mouse input script. The problem I am having is when I click on the first game object ( it moves to the point fine - all good) but when I click on the second game object it doesn't move the second game object instead it moves the first game object to where I clicked. How could I click separate gameobject and move them to different place separately.
Mouse Input Script (Seperate gameobject)
     public PlayerMovement playerMovement;
     public Transform Pointer;
     public SpriteRenderer cursorSprite;
     public GameObject selectedUnit;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, 100))
                 //selected
                 if(hit.collider.tag == "Player")
                 {
                     Debug.Log("Player Selected");
                     selectedUnit = hit.collider.gameObject;
                 }
             //deselected
                 else if (hit.collider.tag == "Terrain")
                 {
                     selectedUnit = null;
                     Debug.Log("Deselection");
                 }
 
                 Debug.Log("Left mouse selected");
         }
 
 
         //Get right mouse button down
         if (Input.GetMouseButtonDown(1) && selectedUnit.tag == "Player")
         {
             // Get the reference to where I am point to
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if(Physics.Raycast(ray, out hit, 100))
             {
                 //If i right on the terrain perform this action
                 if(hit.collider.tag == "Terrain")
                 {
                     //Debugging
                     Debug.DrawLine(ray.origin, hit.point);
                     // gets right clicked position
                     Pointer.position = new Vector3(hit.point.x, Pointer.position.y, hit.point.z);
                     // change isMoving to true & enabled the target position sprite
                         playerMovement.isMoving = true;
                         cursorSprite.enabled = true;
                 }
             }
 
     
         }
 
     }
 
               Player Movement Script (in both player gameobject)
     public bool isMoving;
     public NavMeshAgent agent;
     public Transform pointer;
 
     private void Update()
     {
         if (isMoving) // if isMoving equal to true than perform this action
         {
             agent.SetDestination(pointer.position); // move player to position
             agent.isStopped = false;//safe check
         }
     }
 
              Answer by webcam · May 02, 2017 at 02:50 PM
Hey mate, you're going to have to update your playerMovement variable with the PlayerMovement that is attached to the selected unit. Right now you are always accessing one PlayerMovement script I assume you referenced in the inspector.
 if (Input.GetMouseButtonDown(1) && selectedUnit.tag == "Player")
 {
     playerMovement = selectedUnit.getComponent<PlayerMovement>();   
 
              Your answer
 
             Follow this Question
Related Questions
My player still keep moving when i unparent it form the car 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Camera up and down on TouchField 1 Answer
Rotating a cube horizontal axis 1 Answer