Need help getting troup mechanic to act one by one

Hello, so I am programming a game in which a little troup follows you around, kind of like pikmin, but there’s only like five of them. Now i used this code in the troup units themselves (which i instantiated). Each one gets correctly assigned an order in the number variable, so unit 1 gets the number value “0”, number two gets the value “1” and so on.

The problem is, I want them to stop following in order each time I press Fire1. So with five units it would take 5 clicks for them all to stop following. I calculated if the number asigned to the robot is equal to the number of units that are currently not following (troupWorking, which is a universal value i take from a PlayerPrefs script and is by default zero. The value should always be equal to the number of units that are NOT following the player).
But when i press Fire1, all of the units stop following at the same time, because the troupWorking variable gets instantly equal to the number of units on scene, instead of increasing one by one each time I press the button.

(Checking the Debug.Log, it seems as though the value of the troupWorking does increase one by one, but automatically increases again the next frame. I don’t know if this is because of the GetButtonDown being active for more than one frame, but either way, I can’t seem to find a way to fix it).

Here is the code:

    void Start()
    {
        number = Invocar.troupNum;
        troup = GetComponent<NavMeshAgent>();
        player = GameObject.FindGameObjectWithTag("Player");
        following= true;
    }

    void Update()
    {
        Debug.Log(PlayerPrefs.troupWorking);


        if (Input.GetButtonDown("Fire1"))
        {
            if(number == PlayerPrefs.troupWorking)
            {
                following= false;
                PlayerPrefs.troupWorking++;               
            }

        if (following== true)
            {
                troup.SetDestination(player.transform.position);
            }

If this script is on every single agent, then every single agent will get a button down in a given frame that the button goes down.

If you need behaviour coordinated between agents such that each click makes a particular agent and only that agent do something, make some kind of agent manager or group manager.

Thank you kind sir!!! This helped and the solution was simpler than I imagined. :smile: