A better way to write this script?

Is there a better way to write this script?:

function OnCollisionEnter (collision : Collision) {

if(collision.gameObject.tag=="thing1"){
    Destroy(gameObject);
}

if(collision.gameObject.tag=="thing2"){
    Destroy(gameObject);
}

if(collision.gameObject.tag=="thing3"){
    Destroy(gameObject);
}

if(collision.gameObject.tag=="thing4"){
    Destroy(gameObject);
}

if(collision.gameObject.tag=="thing5"){
    Destroy(gameObject);
}
}

Is there a way I could write it so it'd be like: if(collision.gameObject.tag=="thing4", "thing5", "thing6"..etc){ or if(collision.gameObject.tag=="thing4" or "thing5" or "thing6"..etc){

I don't want to make it so broad as to have just any collision with this one type of thing, each "thing" has to have a unique tag.

Any ideas?

I think the answer to this really depends more on the details that you haven't mentioned.

For example, do you mean any object whose tag starts with "thing"? if so:

if (collision.gameObject.tag.StartsWith("thing")) {

Or maybe you want to identify whether it's a certain type of object (for example, if it has a certain type of script attached?) if so:

if (collision.gameObject.GetComponent("EnemyScript") != null) {

Or perhaps you could assign all your things to a certain layer, and check for that, eg:

if (collision.gameObject.layer == LayerMask.NameToLayer("EnemyLayer")) {

If none of these seem close to what you're looking for, perhaps provide some more detail of the specifics of your project.

Hope this helps!

On top of the suggestion Duck has, you could also do it generically with SendMessage.

So you do `collision.gameObject.SendMessage( "HitBySpecialCollider" )` or whatever message name you want, and if you want that object to be destroyed you put a component on it that simply does

void HitBySpecialCollider()
{
    Destroy( gameObject );
}

You could clean up that code using 'or' ( || ).

if(collision.gameObject.tag=="thing1" ||
   collision.gameObject.tag=="thing2" ||
   collision.gameObject.tag=="thing3" ||
   collision.gameObject.tag=="thing4" ||
   collision.gameObject.tag=="thing5")
{
    Destroy(gameObject);
}

This is saying if ( (tag is 'thing1') or (tag is 'thing2') or ... )

you could also use a repeat loop:

 var tags : String[];

    function OnCollisionEnter(collision : Collision) {

        for(i=0;i<tags.length;i++){
            if(collision.gameObject.tag == tags*){*
 *//do stuff here*
 *Destroy(gameObject);*
 *print(collision.gameObject.name);*
 *}*
 *}*
 *}*
*```*
*<p>so all our tags need to be in the tags array.</p>*
*<p>hope this helps :)</p>*

Awe! I feel so foolish! I had been typing in or and Or and OR thinking why isn't this working? Thank you petroz!

Imagine you have a triangle with tag "thing1", a cube with tag "thing2", a cylinder with tag "thing3", and a sphere with tag "object". You're a player shooting bullets or something and you only want the bullets to be destroyed if they collide with a triangle, cube, or cylinder, but not a sphere or any other object. Sorry if my initial explanation was unclear, but does this make it a little better?