I have a coin spawner spawning coins and silver coins. The coins fly across the screen and if they hit the player, I destroy them and increment a coin counter. Below is the code attached to the player that detects collision and handles the coin count and destroying the object. If I add Destroy(collision.gameObject) to the top of the OnCollisonEnter function, it works great. If I move it down to the part where I check that the name of the object is Coin, it doesn’t work. I used print(collision.gameObject.name) to make sure that the name of the object is Coin and it displays `Coin(Clone) so I’m pretty sure I have the name correct. Can anybody offer some insight as to why the objects aren’t being destroyed? I assume that somehow I’m not casing out the names right, but after doing the print to check, I’m pretty sure the names are fine. Here’s the code:
function OnCollisionEnter(collision : Collision)
{
//Print the name of the Game Object we hit
print(collision.gameObject.name);
//If we hit a coin increment the count and destroy it
if(collision.gameObject.name == "Coin" || collision.gameObject.name == "SilverCoin")
{
if(collision.gameObject.name == "Coin")
{
coinCount = coinCount+1;
Destroy(collision.gameObject);
}
else if(collision.gameObject.name == "SilverCoin")
{
coinCount = coinCount+2;
Destroy(collision.gameObject);
}
}
//If we hit a hazard reset the count and restart the level
if(collision.gameObject.name == "Hazard")
{
coinCount = 0;
Application.LoadLevel ("ProofOfConcept");
hazardCount = hazardCount+1;
}
}