• 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#

This is the player script, can't show all but this 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 this, the object is destroyed so it can't add more points again.
             Destroy(other.gameObject);
         }
     }

This is the Score script that display how much totalCoins i have on gui.

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
 
 
     
     //This is the sound we want to play if a player gets a coin.
     public AudioClip coinSound;
     
     //this is a private variable that we'll set as a saved variable in Start().
     [HideInInspector]
     public static int totalCoins;
     
     void Start (){
         //This sets the private variable to the saved variable we create thats also in this script. It allows us to carry on information that the player has changed by playing.
         totalCoins = 0;
         //This 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();
     }
     
     //This function is called when a coin sends us the message "getCoin" which 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;
         //this 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

Please post a script.

avatar image leandroreschke · May 27, 2014 at 11:15 PM 0
Share

Posted! just the part that involve taking coins, when i take it some times i take 1 and it shows 2, not only shows 2 on GUI, but actually totalCoins become 2 and not 1 for sure.

avatar image meat5000 ♦ · May 28, 2014 at 12:42 AM 3
Share

BIG WARNING, $$anonymous$$Y PLAYER HAS 2 COLLIDERS, ONE BOX2D AND ONE CIRCLE2D I THIN$$anonymous$$ IT'S THIS

Problem solved?

avatar image leandroreschke · May 28, 2014 at 09:29 PM 0
Share

Solved? I know the problem, but not solved it, i give it one collider for now, but the collider is a box that stuck sometimes on floor. Before that i used a box for body and circle for the foot, can you help me?

avatar image meat5000 ♦ · May 28, 2014 at 09:32 PM 0
Share

Stuck on the floor? Are you working in 2D or 3D?

avatar image leandroreschke · May 29, 2014 at 03:32 AM 0
Share

As i said, 2D platform, do you know how to not stuck on floor?

avatar image meat5000 ♦ · May 29, 2014 at 09:50 AM 1
Share

So its a wall rather than a floor, right?

http://forum.unity3d.com/threads/182046-2D-game-movement-issues-Stuck-On-Walls

avatar image leandroreschke · May 29, 2014 at 06:43 PM 0
Share

yes, i'm using box collider for "walls" and a circle collider for ground, but indeed, wall and floor uses box collider, they are the same.

avatar image NoseKills · May 29, 2014 at 08:33 PM 1
Share

This is getting very confusing now. Are you still getting multiple triggerEnters or is your problem getting stuck in the floor ? $$anonymous$$aaybe this is worth another question ?

If i understood right, you are collecting coins with the onTriggerEnter2D ? Give the coin-class a bool variable that tells you whether it is already collected or not perhaps ? Then it can't be collected twice in case the colliders are bouncing against eachother or you have multiple colliders in the player object

avatar image leandroreschke · May 29, 2014 at 09:31 PM 0
Share

I will clarify everything to you, a 2D character in my game, walk in the floor normally because the circle2D collider on his feet have 1 friction, and for walls when they fall from platforms, to not slide slowly and for have a collide in the rest of his body, i use a box collider, more like a retangle. So, they are close to each other, and when i took a coin, the box and the circle have touched the coin and was supposed to touch one collider and destroy, but i think it touched in the same frame. To prove that, i took way 1 collider, and it never happen again, but i need 2, if i use one, i use polygon, that have spike edges like a box right, and it get sometimes stuck on floor, not like the circle collider, the problem is i can't use just a circle collider because my character is not fat, it's slim with about 2 m tall. So, or i learn how to detect 1 collision from 2 colliders, or i learn how to build a 2D with one collider2D, we should have a capsule2D, that could fix my problem, but we don't have and i use collision2D and rigidbody2D in everything, can't mix with 3D collision or Rigidbody 3D.

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

Thanks, hold up, i will try it!

avatar image leandroreschke · May 30, 2014 at 01:30 AM 0
Share

as i expected, your code has a logic problem, when collected it will never turn false again, i think i need to deter$$anonymous$$e it by Time.deltaTime, i don't know

avatar image Alk Studios · May 31, 2014 at 10:59 PM 0
Share

If the object is destroyed as described in my provided code snippet, there is no need to change the value of collected as the object no longer exists. If however you wish to keep the object, you could simply test if the player is still collided, if the player is not, then simply change the value back to false.

avatar image Alk Studios · May 31, 2014 at 11:01 PM 0
Share

Just to be clear, from the context of your original post, I assumed your game involved the collection of coins, after which a collected coin is no longer available. $$anonymous$$y snippet should work for such a situation. If I am wrong let me know and I'll provide a more appropriate answer :)

avatar image leandroreschke · Jun 01, 2014 at 04:59 PM 0
Share

The problem is that your code is needed for every coin in the scene to detect the player, so if i have 5000000 coins, i have 5000000 scripts attached, if you read the first code that is part of my player movement script you will see that is one script attached to player that find coins. I understand your code, but it works only if the scripts is attached to coin object and search for player, then when destroyed your logic work, but for $$anonymous$$e not x.x

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

Scoring System between two different scene? 2 Answers

Highscore Save 0 Answers

How to email highscore through android? 2 Answers

How do I create an apk file 2 Answers

Adding Scores to my script 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