Is there a way to check if a gameobject isn’t colliding with any game object with a tag
Yes, you can use the approach you described to check if a GameObject isn’t colliding with any other GameObjects with a specific tag. By creating a List<Collider>
field and adding/removing colliders to that list on OnTriggerEnter
and OnTriggerExit
, you can keep track of the colliders that the GameObject is currently colliding with
using System.Collections.Generic;
using UnityEngine;
public class CollisionChecker : MonoBehaviour
{
public string tagToCheck = "TagToCheck"; // Replace "TagToCheck" with the tag you want to check against.
private List<Collider> _collidingWith = new List<Collider>();
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(tagToCheck))
{
_collidingWith.Add(other);
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag(tagToCheck))
{
_collidingWith.Remove(other);
}
}
public bool IsCollidingWithSpecificTag()
{
return _collidingWith.Count > 0;
}
}