- Home /
Question by
Mistyisty123 · Jul 21, 2019 at 04:30 PM ·
c#arraylisttags
Array of tags, gettings tags using CompareTag
Hello,
I'm trying to figure out a way to use:
public string tags[];
Then implement the tags in:
hit.collider.CompareTag()
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! :)
Comment
Best Answer
Answer by Vega4Life · Jul 21, 2019 at 05:07 PM
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[i]))
{
return true;
}
}
return false;
}
}