- Home /
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
If I'm understanding your question correctly, you might be able to use a switch. You would just need to find a reference to the specific objects you wish to destroy and add them appropriately.
void Update ()
{
//Just pass in the amount of coins you've collected.
switch (coins)
{
//Destroy at 4 coins collected.
case 4:
Destroy (object1);
break;
//Destroy at 8 coins collected.
case 8:
Destroy (object2);
break;
}
}
If I'm misunderstanding your question, please let me know and I'll try again :)
Hi Guppie,
thank you for your response. I believe you are on the right track! But please excuse me, where do I place this script? On the Player as a separate script? Or on the same script as above?
Have tried both options and referenced it to certain objects but unity is not happy, getting errors.
Sorry for being a pain, I've being trying to wrap my brain around this for days. It's crazy, I can set up a whole character controller with animations, and create a vast world full of codes and colliders.... But this simple task, I'm lost lol. So seriously, thank you for all the help...
You would attach it something like your GameController.
Answer by MrMeows · Oct 25, 2015 at 04:32 AM
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 would use the modulo operator:
if ( (coins % 6) == 0 )
{
}
I would only use a modulo if it were incremented at the same rate and needed to destroy the same expected GameObject (e.g. a coin will always be a coin). The issue is with trying to destroy different GameObjects. In order to make the modulo efficient enough in this case, it would require much more attention that would render it less efficient than the alternate methods.
Answer by arcifus · Oct 28, 2015 at 01:07 PM
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);
}