How to search a LIST(not an array) for game objects with a specific tag?
So I'm trying to build a hierarchy of targets for an enemy AI. I'm using a simple list to gather the objects around the AI, and then I'm trying to access these items properly. My plan is to use tags to differentiate between priority objects
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class AI_Collision_Detection: MonoBehaviour
{
public AI_Spherical_Detectors innerSphere;
public AI_Spherical_Detectors middleSphere;
public AI_Spherical_Detectors outerSphere;
public List<GameObject> innerSphereDetectios;
public List<GameObject> middleSphereDetectios;
public List<GameObject> outerSphereDetectios;
public GameObject avoidanceFocus;
private void Update()
{
innerSphereDetectios = innerSphere.detections;
middleSphereDetectios = middleSphere.detections;
outerSphereDetectios = outerSphere.detections;
if (outerSphereDetectios.Contains(gameObject.transform.tag ("Barrier")))
{
Debug.Log("found");
}
So this is basically what I'm working with(I cut out some raycasting bits. This part in particular is where I'm having my trouble.
if (outerSphereDetectios.Contains(gameObject.transform.tag ("Barrier")))
{
Debug.Log("found");
}
I've tried many different way to get it to function properly. The only time I got it to slightly work was with this.
if (outerSphereDetectios.Find(i => i == GameObject.FindGameObjectWithTag("Barrier")))
{
Debug.Log("found");
}
However, only a single barrier in the scene was setting off the if statement. All of the other ones do nothing for it. I've tried running Contains, Exists, Find, and FindAll; but none of them seem to be working the way I want them too.
Answer by tormentoarmagedoom · Oct 18, 2018 at 10:51 AM
How to search a list for game objects with a specific tag?
With this:
GameObject[] ObjectsArray = GameObject.FindObjectsWithTag("TheTag");
Look that this are 2 differnt functions:
FindObjectsWithTag -> Returns an array
FindObjectWithTag -> Returns the first object found
Bye!
This is not what I need. I'm using a list because the number of object in my detection radius will change. So I want to deter$$anonymous$$e if an object with a certain tag is in this radius. Simply having an array with all tagged objects in it does nothing for me.
Your answer
Follow this Question
Related Questions
Detecting Enemy Distance via List 2 Answers
Horror AI looks for player 0 Answers
Saving And Loading List error 1 Answer
How do i randomize certain waypoints at the end of a path? 0 Answers