Find the first game object created with a given name

What I’m trying to do here is get a list of objects instantiated with the same name and choose a leader from those, the leader would be the first one instantiated and would be given a new tag “Leader”.

so far I’ve got a pretty simple piece of code:

if(GameObject.FindWithTag("Leader")==null)
    tag="Leader";

the problem being that rather than just look within a group of objects with a similar name, my script looks through all the game objects in the game for one with the tag leader, meaning I end up with 1 leader instead of 1 per group (there’s a group per spawn point), is there any way I can further restrict my if statement to only search through game objects with a given name?

If you have a set of already created game object and you have not created any leaders, you can do this:

var go = GameObject.FindWithTag("TagWhenCreated");
if (go != null) {
    go.tag = "Leader";
}

Note ‘TagWhenCreated’ is whatever tag you gave the game object when you instantiated it. This code will give one object the ‘Leader’ tag. It may not be the first one instantiated. If you absolutely need the first one to be the leader, then you need to do it at instantiate time. Here is the pseudo-code for one way to make it happen:

var go = Instantiate(prefab, position, rotation) as GameObject;
if (GameObject.FindWithTag("Leader") == null) {
    go.tag = "Leader";
}

This assumes that the tag in the prefab is not “Leader”.