Objects of the same prefab are acting differently

This question was first asked on Unity answers, sorry for copying directly :wink:

I’m finishing up the pathfinding for my RTS game. When you right click, the selected units are supposed to move to the current mouse position. For some reason this only works for 3 of my player-objects (out of 6). The rest will act correctly when I have clicked a second time, which doesn’t make much sense to me since they are all objects of the same prefab.

If I select all units, and rightclick somewhere only 3 of them will react every time, the others after every second click - hope you understand now :slight_smile:

I am using the A* pathfinding algorithm by Aaron Granberg (haven’t asked about help on their forum yet). I am using a grid graph with a “raycast modifier” for all my player units.

Here are some lines of code where I think the problem might be:

Mouse.cs (attached to an empty gameobject)

`    if(Input.GetMouseButtonDown(1)) { // if right click
    GameObject newPosition = GameObject.Find("RightClickTarget"); //This is the actual target for a Unit object
    GameObject targetObj = GameObject.Find("Target Indicator"); // This is just an indicator to the player that pops up wherever he rightclicks
    newPosition.transform.position = hit.point; // hit = RaycastHit - the unit's target moves to the position clicked
    targetObj.transform.position = hit.point;
    GameObject animator = targetObj.transform.Find("Mouse Point Object").gameObject;
    animator.animation.Stop();
    animator.animation.Play("Mouse Target Sink");
    }`

AIPath.cs (this script is attached to every player unit in the game. This script was downloaded from Aaron Granberg’s A* project, and I’ll admit that I don’t understand all of the code in this script. So feel free to correct me if I am doing something horribly wrong - but here is a snippet where I tried to add something myself)

`    /** Requests a path to the target */
    public virtual void SearchPath () {
    canSearch = false;
    if (target == null) throw new System.InvalidOperationException ("Target is null");
     
    lastRepath = Time.time;
     
    Unit unitScript = this.GetComponent<Unit>();
     
    // if unit is selected and right mousebutton down - search new path
    if (unitScript.selected == true && Input.GetMouseButtonDown(1)) {
     
    //This is where we should search to
    Vector3 targetPosition = target.position;
    oldPosition = targetPosition;
     
    canSearchAgain = false;
     
    //Alternative way of requesting the path
    //ABPath p = ABPath.Construct (GetFeetPosition(),targetPoint,null);
    //seeker.StartPath (p);
     
    //We should search from the current position
    seeker.StartPath(GetFeetPosition(), targetPosition);
     
    }
    }`

Here is a link to the rest of the script if you dare reading that: http://pastebin.com/p329Lgmr
Link to the same question on Unity answers answers.unity3d.com/questions/632998/objects-of-the-same-prefab-are-acting-differently.html

So can anyone explain why this works for some of my objects, while others need a second click?