Player collects X amount of coins - Object is destroyed

Hi,

I need a little help with coin collecting. I have that down so far with this piece of Javascript…

var coins : float;

function OnTriggerEnter( other : Collider ) {
    if (other.tag == "coin") {
        coins += 1; // or however many points you want to give per coin
        Destroy(other.gameObject);
    }
}

function OnGUI () {

        GUI.Label (Rect (20, 20, 200, 40), coins + "");

    }

But what I need now is for the code to destroy an object when it gets to 6 coins collected, another when 12 coins collected, another when 18 coins… etc, you get the picture. The code could be on the actual object itself or attached to this piece of coin script I have already, which is the correct way of doing this?

I’ve tried a few bits of code just from knowledge to get this to happen but I’m not having much luck… A little bit of help would be much appreciated. Thank you in advance,

Yours kindly,

Adam

function OnTriggerEnter( other : Collider ) {
if (other.tag == "coin") {
coins += 1; // or however many points you want to give per coin
Destroy(other.gameObject);
if(Mathf.Ceil(coins / 6) == coins / 6) {
Object.Destroy(someObject);
}
}
}

I do not use JavaScript, so forgive me if I did that wrong. The if statement tests whether coins is divisible by 6. Note that it will return true if coins == 0, but unless coins are ever subtracted, that situation will not occur.

I’m assuming that in your game different objects give out different number of coins and when they run out, you destroy them.

In that scenario, the correct way of doing this is to create script, called something like CoinHolder with a public int variable inside the script called currentCoins. Attach the CoinHolder script to your objects that give out coins.

You can then initialise different CoinHolder objects with different values based on how many they give out.

Next in your collection script do something like this (I’m not familar with javascript syntax):

coins += 1;

CoinHolder coinHolder = other.gameObject.GetComponent<CoinHolder>;
coinHolder.currentCoins -= 1;

if ( coinHolder.currentCoins == 0 ) {
   Destroy(other.gameObject);
}