• 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
-1
Question by leandroreschke · May 27, 2014 at 09:12 PM · androidscorepointsontriggerenter2d

Problem with duplicated score!

I'm using a script to add score, the script is simple it uses a OnTriggerEnter2D to detect player collider enter and add += 1 to the score, but some times if a pass faster on 4 coins for example it ads 6, 5, 7, it duplicate, i don't know why! C#

T$$anonymous$$s is the player script, can't show all but t$$anonymous$$s is the score part:

 void OnTriggerEnter2D ( Collider2D other  ){
         if(other.tag == "coin"){
             //If the player touches a coin, it will find the object that manages the coin count and tells it that a coin was taken.
             GameObject score = GameObject.Find("Score");
             score.BroadcastMessage("getCoin", SendMessageOptions.DontRequireReceiver);
             //after t$$anonymous$$s, the object is destroyed so it can't add more points again.
             Destroy(other.gameObject);
         }
     }

T$$anonymous$$s is the Score script that display how much totalCoins i have on gui.

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
 
 
     
     //T$$anonymous$$s is the sound we want to play if a player gets a coin.
     public AudioClip coinSound;
     
     //t$$anonymous$$s is a private variable that we'll set as a saved variable in Start().
     [HideInInspector]
     public static int totalCoins;
     
     void Start (){
         //T$$anonymous$$s sets the private variable to the saved variable we create thats also in t$$anonymous$$s script. It allows us to carry on information that the player has changed by playing.
         totalCoins = 0;
         //T$$anonymous$$s sets the Text in the top right Corner to display how many coins we have depending on the variable totalCoins. Ex. COINS: 16
         transform.guiText.text = "COINS: " + totalCoins.ToString();
     }
     
     //T$$anonymous$$s function is called when a coin sends us the message "getCoin" w$$anonymous$$ch is in the coin.cs script.
     public void getCoin (){
         //once we receive the message from the coin, we play the sound we set when a coin is taken.
         audio.PlayOneShot(coinSound);
         //we add 1 to totalCoins
         totalCoins += 1;
         //t$$anonymous$$s updates the text in the top left corner again, just like in method Start()
         transform.guiText.text = "COINS: " + totalCoins.ToString();
     }
 }


##BIG WARNING, MY PLAYER HAS 2 COLLIDERS, ONE BOX2D AND ONE CIRCLE2D I THINK IT'S THIS, BUT HOW TO THE SCRIPT DETECT 1 COLLIDER, OR HOW TO MAKE A 2d CHARACTER WITH 1 COLLIDER Oo##

Comment
Add comment · Show 10
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 Kiwasi · May 27, 2014 at 09:13 PM 1
Share
avatar image leandroreschke · May 27, 2014 at 11:15 PM 0
Share
avatar image meat5000 ♦ · May 28, 2014 at 12:42 AM 3
Share
avatar image meat5000 ♦ · May 29, 2014 at 09:50 AM 1
Share
avatar image NoseKills · May 29, 2014 at 08:33 PM 1
Share
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alk Studios · May 29, 2014 at 08:44 PM

Try adding the collision detection to the coin objects and then use a boolean flag before destroying the game object.

 private bool _collected = false;
 
 void OnTriggerEnter2D(info : Collider2D) 
 {
     if (info.tag == "Player")
     {
         if (!_collected)
         {
             totalCoins += 1;
             _collected = true;// Not needed if destroying the game object
             Destroy(t$$anonymous$$s.gameObject);// Only use if you intend to destroy the game object
         }
     }
 }
Comment
Add comment · Show 6 · 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 leandroreschke · May 29, 2014 at 09:31 PM 0
Share
avatar image leandroreschke · May 30, 2014 at 01:30 AM 0
Share
avatar image Alk Studios · May 31, 2014 at 10:59 PM 0
Share
avatar image Alk Studios · May 31, 2014 at 11:01 PM 0
Share
avatar image leandroreschke · Jun 01, 2014 at 04:59 PM 0
Share
Show more comments

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

24 People are following this question.

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

Related Questions

GUI Button doesn't Work on Android 2 Answers

Unity Admob Banner is not clickable. 0 Answers

Share score on facebook *Without facebook SDK* (ANDROID) 2 Answers

Android Java Proxy could not be found. 0 Answers

can xml be used in anrdoid?? 1 Answer


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