Where did I go wrong (logical operator or tagging)

So I have a script attached to my player that evaluates an object when it collides with it, if it's a point pickup, it adds 10 to the score and if it's an enemy object it takes away health. I also have it set up to destroy the pickup when it collides with it and start an explosion prefab. Everything works fine if I just have the one object set to explode, but when I tried to code in the tag for the second object my player object started exploding and destroying my walls as well even though they are set to Untagged. So I'm making a misstep somewhere, I just don't know where.

var explosion : GameObject;
 var Shiny : AudioClip;
 var Dull : AudioClip;

 function OnCollisionEnter (hit : Collision) {
 if(hit.gameObject.tag == "Life" || "Score"){
 var expPos = hit.transform.position;
    var explosion : GameObject = Instantiate (explosion, expPos, Quaternion.identity);
 LifeScript.life--;
 Destroy(hit.gameObject);
  }
 if(hit.gameObject.tag == "Life"){
 audio.clip = Dull;
 audio.Play();
  }
 if(hit.gameObject.tag == "Score"){
 scorer.scorz = scorer.scorz + 10;
 Destroy(hit.gameObject);
  }
 if(hit.gameObject.tag == "Score"){
 audio.clip = Shiny;
 audio.Play();
  }
 }

I know it's got to be something simple. As always help is appreciated.

if(hit.gameObject.tag == "Life" || "Score")

You're checking:

  • if hit.gameObject's tag is "Life" OR
  • if "Score"

Strings always evaluate as true.

You want:

if(hit.gameObject.tag == "Life" || hit.gameObject.tag == "Score")


As a side-note, you could generally tidy up the code quite a bit. Also, you'll probably get a warning if the object is tagged "Score", because you're checking the tags after destroying it in the first ifcheck, and later destroying the same object again.

Here it is rewritten:

function OnCollisionEnter (hit : Collision)
{
   if( hit.gameObject.tag == "Life" || hit.gameObject.tag == "Score" )
   {
      var expPos = hit.transform.position;
      var explosion : GameObject = Instantiate (explosion, expPos, Quaternion.identity);
      LifeScript.life--;

      if( hit.gameObject.tag == "Life" )
         audio.clip = Dull;
      if( hit.gameObject.tag == "Score" )
      {
         scorer.scorz = scorer.scorz + 10;
         audio.clip = Shiny;
      }
      audio.Play();
      Destroy(hit.gameObject);
   }
}