Need an advise, how to find all objects with a specific name and a specific script?

So i have two scripts. Script Leaders is attached to some objests on the scene,script Follower is to move an object to objects with attached script Leaders.If i have one object everything works fine,im clicking at object for examole a cube, than im clicking at a spot,its moving to this spot. But if im having two or more objects,only one is moving or doing something.This happening i guess because its finding only one object
follower = GameObject.Find(“Follower”).GetComponent< Follower>(); So my question is, how to make this line like to be something like an array to store information of all objects on the scene? Thanks for ant help.

public class Leaders : MonoBehaviour

{

private Follower follower;

void Start ()
{	   
   follower = GameObject.Find("Follower").GetComponent< Follower>();			
}

void OnMouseDown() 
{	
	if(follower.selected == true)
	{
    follower.v3Dest = transform.position;
	follower.selected = false;
	}				
}

public class Follower : MonoBehaviour

{

public Vector3 v3Dest;

public bool selected = false;

public float speed = 5.0f;

void Start () 
{
   v3Dest = transform.position;
}


void Update () 
{		
   transform.position = Vector3.MoveTowards(transform.position,v3Dest,Time.deltaTime * speed);
   transform.LookAt(v3Dest);	 		
}
void OnMouseDown()
{		
	selected = true;
}

}

GameObject.Find() finds only one GameObject. With GameObject.FindGameObjectsWithTag() you can find every Game Object with a specific tag.

Beware: Find functions are very slow and shouldn’t be used too often (or even repetitively in Update()…)

It’s way easier to directly search for your Follower scripts. Just use FindObjectsOfType:

public class Leaders : MonoBehaviour
{
    private Follower[] followers;

    void Start ()
    {
        followers = FindObjectsOfType(typeof(Follower)) as Follower[];
    }
    void OnMouseDown()
    {
        foreach(var follower in followers)
        {
            if(follower.selected == true)
            {
                follower.v3Dest = transform.position;
                follower.selected = false;
            }
        }
    }
}

ps you might want to rename your “Leaders” class to “Leader” because one instance is just a leader :wink: