How to exclude self from FindGameObjectsWithTag

I’m writing a script to make NPCs randomly select another NPC to talk to. Currently I’m using the following:

	AIPath npcPath = npc.GetComponent<AIPath> ();
	GameObject[] desiredObject = GameObject.FindGameObjectsWithTag("NPC");
	Transform[] desiredTransforms = Array.ConvertAll<GameObject, Transform>(desiredObject, x => x.transform );

	npcPath.target = desiredTransforms(UnityEngine.Random.Range(0, desiredTransforms.Length);

The problem is, there is nothing preventing the NPC from talking to himself. Is there an easy way to exclude the NPC the script is attached to from the findgameobjectswithtag call?

I don’t think there’s any built-in way to exclude self, since it is correct for FindGameObjectsWithTag to return the current object if it has the specified tag.

You could do something like this:

int randomIndex = -1;
do
{
    randomIndex = Random.Range(0, desiredTransforms.Length);
}
while(desiredTransforms[randomIndex].gameObject == gameObject);

Another way to do this would be to iterate over all Transforms in desiredTransforms and remove the one where transform.gameObject == gameObject.