- Home /
How to check a collided gameobject for valid tags?
I'm creating a teleport script that only teleports gameobjects with certain tags to be allowed to use it. I am using an array for a list of valid tags to activate the gameobject to teleport.
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player") {
TeleportEffects (enterParticles, other.transform.position, other.transform.rotation);
if (exit == null) {
other.transform.position = newPosition;
other.transform.rotation = Quaternion.Euler (newRotation);
}
else {
var otherController = other.GetComponent<CharacterController>().height;
other.transform.position = exit.position + Vector3.up * otherController / 2;
other.transform.rotation = exit.rotation;
}
TeleportEffects (exitParicles, other.transform.position, other.transform.rotation);
if (telesound != null)
AudioSource.PlayClipAtPoint (telesound, other.transform.position);
I want to replace the "Player" string with public string[] validTags . How do I replace this and check all the array elements when the you enter the trigger box?
Answer by Eric5h5 · Aug 16, 2013 at 12:36 AM
No reason to use loops, just use Contains if you're using a List:
if (validTags.Contains(other.gameObject.tag)) {
Or IndexOf for an array:
if (System.Array.IndexOf(validTags, other.gameObect.tag) != -1) {
Answer by hamcav · Aug 15, 2013 at 11:49 PM
I would do the following
void OnTriggerEnter(Collider other) {
for(int i = 0; i < validTags.Length; i++){
if (other.gameObject.tag == validTags[i]) {
...
...
...
}
}
}
//sure you can
foreach(string _tag in validTags){
...
...
...
}
Your answer
Follow this Question
Related Questions
Finding all Game Objects with multiple Tags 4 Answers
Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers
My array does not update when object is destroyed. How do I fix it? (java) 2 Answers
Checking for closest gameobject with tag. 5 Answers
Making an array that contains object with one of 2 tags 2 Answers