- Home /
'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.
Answer by GesterX · May 03, 2011 at 11:41 PM
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...
- You hit a danger area
- The code checks whether the danger area is tagged as coin
- It is not so it goes to the "else" part of the code
- 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
Answer by Owen-Reynolds · May 03, 2011 at 11:33 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers
For loop on collision (two hit kill) 2 Answers
Leap motion: how to pickup a custom mesh 0 Answers
In-game pickup not moving towards player after being collected 1 Answer
How to add health pickup/potion 2 Answers