- Home /
Best way to list multiple tags...
Hi all, I hate to be a bother, but I'm stuck. In a nutshell: I have a prefab cannonball that is instantiated and shot at multiple targets passing in front of a cannon when the player fires. Each target has a different tag name for gameplay reasons. I can't figure out how to write the script (js) so that the cannonball will explode only when it hits a gameobject collider with one of the appropriate tag names.
The script below works fine, but I'm limited to only one tag name, and I know there is a better way than putting a script for each tag on one prefab cannonball, I'm just too new at scripting to know what it is. I have searched answers, and have found solutions regarding arrays and/or variables, but none address tags specifically, and the required syntax is beyond me.
function OnCollisionEnter(myTarget : Collision) {
if(myTarget.gameObject.tag == "Orc") {
Kaboom();
}
}
Thanks for any/all assistance.
P.S. Compared to most of you guys, I'm ancient and probably old enough to be your grandfather, so please be gentle :)
Answer by Eric5h5 · Feb 27, 2013 at 03:04 AM
You can use Array.Contains:
import System.Linq; // So Array.Contains will work
var tags = ["Orc", "Goblin", "GardenGnome"];
function OnCollisionEnter (myTarget : Collision) {
if (tags.Contains(myTarget.gameObject.tag)) {
DieMonsterDie();
}
}
A tag is a string, so anything relating to strings and arrays thereof also applies to tags.
Damn, I was so close in my sad efforts...got the var part right, but couldn't get the if statement...$$anonymous$$d you, it wouldn't have worked anyway, I have never seen that 'import System.Linq' before.
Thanks very much Eric; you sir, are a gentleman, it worked like a charm.
It's still got me knackered how you youngsters can take in and remember what has got to be thousands upon thousands of different options for this coding stuff...and then just to rub it in you learn more than one code! :) $$anonymous$$akes us old blue-collar fogies feel totally inadequate...I was born 35 years too early lol.
remember what has got to be thousands upon thousands of different options for this coding stuff
Heck no, that's what the docs and/or Google are for. ;) There's too much to remember; you just have to be effective with search terms. (Which still involves memory, to be sure, but not as much as memorizing all the functions, which I certainly haven't done by a long shot.) Anyway you knew what sort of thing you were looking for, which is at least half the battle, so don't sell yourself short.
Answer by Tarlius · Feb 27, 2013 at 05:56 AM
You can also use | or || , the OR operators The difference being || will skip over the rest of the matches when it finds a true, while | will perform all the tests regardless.
var tag = myTarget.gameObject.tag;
if (tag == "Orc" ||
tag == "Goblin" ||
tag == "GardenGnome") {
Eric5h5's solution is much more elegant for this, but it is worth noting that this structure also exists as it will be useful in many other situations.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093453im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to get a variable like Layermask but for tags 2 Answers
Create new variable name based on array index 3 Answers
How can I make variables reset when leaving play mode? 3 Answers
Clicking on a Cube. Then -1 a varible 1 Answer
Variable Vs Array? 2 Answers