• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Wreck-Tangle-Games · Oct 25, 2015 at 12:24 AM · javascriptcoinscollectible-game-objects

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

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Guppie1337 · Oct 25, 2015 at 03:54 AM 0
Share

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 :)

avatar image Wreck-Tangle-Games Guppie1337 · Oct 25, 2015 at 11:38 AM 0
Share

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...

avatar image Guppie1337 Wreck-Tangle-Games · Oct 27, 2015 at 11:56 PM 0
Share

You would attach it something like your GameController.

2 Replies

· Add your reply
  • Sort: 
avatar image

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.
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saschandroid · Oct 28, 2015 at 08:08 AM 0
Share

I would use the modulo operator:

 if ( (coins % 6) == 0 )
 {
 }
avatar image Guppie1337 saschandroid · Oct 28, 2015 at 12:15 PM 0
Share

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.

avatar image

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);
 }
 
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

I am trying to create a script that saves score i have tried using playprefs but i am not to familiar on them to know just how to use them 1 Answer

How do I instantiate a GUI Texture at set co-ordinates 0 Answers

"Object reference not set to an instance of an object" when attempting to parse Dictionary of Lists with JSONFx, MiniJSON 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges