Exclude self in 'findgameobjectswithtag' ?

I have multiple characters with the same tag that needs to pick a random other character and interact with them. Kind of like how the puppies in nintendogs would randomly pick another dog to play with. How do I exclude them from including themselves in the found objects?


enter code here

var peoples:GameObject;
var person : GameOject;

peoples = GameObject.FindGameObjectsWithTag(“Humans”);

function Interact(){
person = peoples[Random.Range(0, peoples.length)] // (except yourself somehow…);
// go and talk to random person


thank you

An easy fix would be to just tag your player to something other than “Human”. However, im sure you already structured code to use this method for regularly called functions. Sounds like you will need to switch to Array(), convert the array to be re-sized then convert back, or use the Generic List so you can remove the slot in the array. Again, if possible, just switch the tag of your player.

Hey there! :slight_smile: I have a pretty late answer (5 years :smiley: ), but someone who stumbles upon this may found use of it.

	var people : GameObject[];
	var person : GameObject;

	function Update(){
		this.gameObject.tag = "Counter"; //temporary tag
		people = GameObject.FindGameObjectsWithTag("Human"); //find everything
		Debug.Log("Found "+people.length+" people.");
		this.gameObject.tag = "Human"; //change your tag back

	function Interact(){
		person = people[Random.Range(0, people.length)];
		// go and talk to random person
		Debug.Log("Random person: "+person.name);