Help with individual selection and deselection

I am attempting to use the same script on multiple game objects. The problem I am having is that I cannot change the script on a manager code which tells the selected item to move. What is happening is that only one is able to move, and it has to be selected for the other one to move at all. Any way to intercommunicate between the same scripts on different game objects and with the manager script? Here are the fragments of the code that are affected:
Script that is being put on individual gameObjects:

void OnMouseDown()
    {
        if(gameObject.tag == "Serf")
        {
            showHealth = true;
            isSelected = true;
            gm.mover = this.gameObject.transform;
        }
        else
        {
            showHealth = false;
            isSelected = false;
        }
    }
 
    void OnGUI()
    {
        if(showHealth == true)
        {
            GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth.ToString());
            GUI.Box(new Rect(10, 30, hungerBarLength, 20), curHunger.ToString());
            GUI.Box(new Rect(10, 50, happyBarLength, 20), curHappy.ToString());
            GUI.Box(new Rect(10, 70, fearBarLength, 20), curFear.ToString());
        }
    }

And the GameManger code, “ph” being the code above:

    void Update () {
        if(Input.GetMouseButtonDown(1))
            isSelected = false;
        if(ph.isSelected == true)
        {
            move();
        }
 
    }

I do not know what I may be doing wrong, and I was thinking about using an array, but I am not completely sure what may be the problem. Any advice would be appreciated.

Both the correct behavior and the problem behavior are unclear here. In posting a question, you have to remember that we don’t see or play with your app, so you have to describe things in detail. A couple of suggestions.

  • The general consensus is that static variables cause problems and are to be avoided. I find in some specific situations they simplify a problem, and this is one of them. If you had a static variable that stored some unique aspect of the game object (gameObject or transform or some other specific component), you could assign that unique attribute in OnMouseDown() to denote that the object is selected. That way it is easy for each object to check if it is the selected one.
  • You execute the move() code in the manager script. I’m assuming you are moving individual objects with this code. Consider moving the moving the movement code to the script on individual objects. That is each object needs to know how to move itself.

Here is a bit of guessing at some code:

private static Transform selected;

void OnMouseDown()
    {
        if(gameObject.tag == "Serf")
        {
            showHealth = true;
            selected = transform;
            //gm.mover = this.gameObject.transform;
        }
        else
        {
            showHealth = false;
        }
    }

public static Transform GetSelected() {
    return selected;
}

public static void ClearSelection() {
    selected = null;
}

Then in your GameManage script you can do:

    if(Input.GetMouseButtonDown(1))
        Whatever.ClearSelection();

…where ‘Whatever’ is hte name of the script on the individual objects.