Hello
I have run into a problem that has stumped me a bit. I have some code that returns an array of colliders, and I need to be able to check if an object with a specific tag exists in that array, and if it does, set some kind of value and then change the value back when there no longer is an object with that specific tag in the array anymore. But the problem is that I can’t figure out a way to do this.
So to make the actual question more clear.
- How can I check if one or multiple objects with a specific tag exists in a array?
- How can I detect if no object(s) with a specific tag exists?
If it is to any help or anything, this is the code I use to get the objects.
private Collider[] m_colliders;
public LayerMask m_layers;
// Update is called once per frame
void Update()
{
m_colliders = Physics.OverlapSphere(transform.position, 0.5f, m_layers, QueryTriggerInteraction.Ignore);
}
EDIT: It just turns out I’m a bit of an idiot. The way I thought was wrong was actually right, i.e, looping through the objects. So I just added this piece of code that did the trick for my situation.
bool ContainsTag(string tag)
{
for (int i = 0; i < m_colliders.Length; i++)
if (m_colliders*.tag == tag)*
return true;
return false;
}