how do i find game objects that can be either in tag A or tag B?
lets say i got 3 factions in a game and some NPC from faction A needs to attack npcs from faction B and C, it needs to find the closest enemy that is tagged either as faction B or faction C
i tried GameObject.FindGameObjectsWithTag([“A”, “B”]) and it gave me an error
i changed “FindGameObjectsWithTag” to “FindGameObjectsWithTags” (added an “s” at the end) and the error went away, i start the game and it doesnt work and im getting another error
There’s no way to do a search for multiple tags at once. You’re going to have to do this in a more roundabout way. There are a few options:
Give all the faction NPCs the same tag, so you can find NPCs from all three factions. Then have something on each NPC (a field in a script somewhere) that identifies its faction. So you can then search through the list looking for enemy NPCs and find the closest. The problem with this is that you’re going to get the NPCs in faction A as well, which gives you more to search through.
Do two searches: one for B, one for C. Find the closest B, then see if you can find a closer C. The downside to this is that you’re running two tag searches, which will take longer. If you’re not running this code too often, that shouldn’t be much of a problem.
Avoid FindGameObjectsWithTag completely. Have a manager script for each faction that keeps track of all NPCs for that faction. When an NPC is spawned or killed, update the list in the manager. That way you can get a list of all B NPCs and all C NPCs without having to do a tag search. This requires a bit more setup, and takes slightly more work on spawn/despawn, but it will save having to do the search (which will get more expensive the more GameObjects there are to look through), and will also therefore save creating a new array for the garbage collector to clean up every time you run the ‘select target’ code. You could even write a method in the faction manager script that returns the closest member of the faction to the given co-ordinates, which will make the code easy to reuse. (This would also be a bit safer, because you wouldn’t have to have the list of faction members directly accessible outside the manager script.) This could well turn out to have other benefits - do you want to be able to quickly see which faction has the most NPCs alive, as some measure of who’s winning? You’d have that information to hand without having to run yet another tag search.
This might be a bit old and a answer got accepted, but I do have an easy way of handling this kind of situation where you simply wish to have factions as tags.
This is in C#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class **Scriptname** : MonoBehaviour {
//First, Add a public list.
public List<Transform> AvailableTargets;
//For the sake of making this script independent, I'll added a public faction int value. This is a cheap and easy way of allowing you to select the NPC's faction through the Inspector.
public int Faction;
//0=Faction A 1=Faction B 2=Faction C
//On Start, create the list of targets.
void Start (){
//This will create the list.
AvailableTargets = new List<Transform>();
//This is a called function that will add all the targets.
AddAllTargets();
}
//This is the called function in the Start.
public void AddAllEnemies(){
if(Faction ==0){
GameObject[] go1 = GameObject.FindGameObjectsWithTag("B");
foreach(GameObject enemy in go1){
AddTarget(enemy.transform);
}
GameObject[] go2 = GameObject.FindGameObjectsWithTag("C");
foreach(GameObject enemy in go2){
AddTarget(enemy.transform);
}
}
if(Faction ==1){
GameObject[] go = GameObject.FindGameObjectsWithTag("A");
foreach(GameObject enemy in go){
AddTarget(enemy.transform);
}
GameObject[] go2 = GameObject.FindGameObjectsWithTag("C");
foreach(GameObject enemy in go2){
AddTarget(enemy.transform);
}
}
if(Faction ==3){
GameObject[] go = GameObject.FindGameObjectsWithTag("A");
foreach(GameObject enemy in go){
AddTarget(enemy.transform);
}
GameObject[] go2 = GameObject.FindGameObjectsWithTag("B");
foreach(GameObject enemy in go2){
AddTarget(enemy.transform);
}
}
}
//This is the small called function that add the listed enemies based on their faction(tag) into the Available Target list.
public void AddTarget(Transform enemy)
{
AvailableTargets.Add(enemy);
}
Note that I written the code based on a tutorial (RPG tutorial by BurgZerg Arcade) which I modified to fit multiple factions. While, in this one, you get the transform values of the opposite faction’s target, you can modify it easily to get anything else.
Well, it works like a charm for me and should gives you the ability to find a easy way to uses tags as fractions.