Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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 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

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

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

Running android app on version 4.0.3 crash? 0 Answers

Scripts don't work when i try app 2 Answers

Camera problem 2 Answers

Scene rendering problem on iOS and Android. 0 Answers

Shadows problem with mobile. 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