Array of tags, gettings tags using CompareTag


I’m trying to figure out a way to use:

public string tags[];

Then implement the tags in:


Instead of doing:

if(hit.collider.CompareTag("Tree") || hit.collider.CompareTag("Logs") || hit.collider.CompareTag("Animal"))

However I can’t find a way to put the tags in CompareTag that work, what would be the best way of doing this or going around this? Thank you! :slight_smile:

Something like this maybe. Method takes your hit, then compares it in a loop. and to be even more awesome, just make it an extension method for colliders.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CompareTag : MonoBehaviour
    [SerializeField] string[] tags;

    // Returns true if hit has the tag
    private bool CheckTag(RaycastHit hit)
        Collider coll = hit.collider;
        for (int i = 0; i < tags.Length; i++)
            if (coll.CompareTag(tags*))*

return true;

return false;