'Deadly' area disappears on collision.

I have an area on my terrain which if hit, you die/loose health. Only problem is, when you do it disappears. The problem comes from the fact i have an on collision pick up script for my 'coins' which when you collide with them, they disappear.

My pick up code is as follows -

var score = 0;
var scoreText = "Score: 0";
var mySkin : GUISkin;
var wall : GameObject;

function OnTriggerEnter( other : Collider )
{
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin")

{
Debug.Log("Other object is a coin");
score += 1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
else
{
score +=5;
scoreText = "Score: " + score;
Debug.Log("Score is now " +score);
Destroy(other.gameObject);
}

if (score >= 5)
{
Destroy(wall);
}
    }

function OnGUI () {
GUI.skin = mySkin;
GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }

The code i have on my hot spot is -

var spawn : Transform; 

function OnTriggerEnter(other : Collider)
{
    if (other.tag == "Player")
    {
        other.transform.position = spawn.position;
    }
}

I've confused myself trying to read through that and figure out what to do. Any help would be greatly appreciated.

That first script is on the player, right? Right now, hitting a coin is worth 1, but, for no good reason I can see, hitting a wall, tree, rock ... is worth 5. If you got rid of the `else`, the code would say to do nothing if you hit a non-coin.

If you want coins to have different values, you could make a "bigCoin" tag and also check for that. Or you could give coins a tiny script with just `var coinVal = 1;` (change in inspector.) The player would then add that coin's value.

The reason it disappears is because you have this:

if (other.tag == "Coin")

{
Debug.Log("Other object is a coin");
score += 1;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
else
{
score +=5;
scoreText = "Score: " + score;
Debug.Log("Score is now " +score);
Destroy(other.gameObject);
}

Your code is doing this...

  1. You hit a danger area
  2. The code checks whether the danger area is tagged as coin
  3. It is not so it goes to the "else" part of the code
  4. In your else code you are saying add 5 to the score, update the GUI, print to the debug log, and then destroy the object the collider is attached to (i.e. the danger area)

You need to either tag your danger areas and do a check in your code for the tag or you could create a new script all together for danger areas (if they are going to have a lot of functionality that other objects won't)

EDIT - I would remove the destroy call from the else part of the statement all together