Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Sep 27, 2018 at 02:52 PM by cspecht for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by cspecht · Sep 27, 2018 at 09:06 AM · collision detectiondetectionscore system2d platformer

2D Object Pickup - Occasionally adding score twice

Hi all, I'm creating my first 2D platform game and having a blast. Things have been going well. However, I've hit a problem with scoring...

Each time the player collects a gem, score increases by 10, and the UI element is updated. Roughly 80% of the time this happens as desired, but the other 20% of the time the +10 is added twice for the same gem. Each play through the issue occurs on a different gem, so it's not a duplicate placement of gem prefabs. Score is controlled through two simple C# scripts:

Score:

public class Score : MonoBehaviour {

     public static int scoreValue;
     Text scoreText;
 
     void Start () {
         scoreValue = 0;
         scoreText = GetComponent<Text>();
     }
 
     private void Update()
     {
         scoreText.text = scoreValue.ToString();
     }
 }

And on the Gem itself:

 public class GemScript : MonoBehaviour {
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag.Equals("Player"))
         {
             Destroy(gameObject);
             Score.scoreValue += 10;
             Debug.Log("Collected - score" + Score.scoreValue.ToString());
         }
     }

The Debug.Log shows in the console that the score is added twice, in the same second. As if the OnCollisionEnter2D method is being called twice for the same collision. I've switched both player and gems between Discrete and Continuous collision detection with no change in behaviour.

Help? Please?

Comment
Add comment · Show 2
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 eses · Sep 27, 2018 at 01:56 PM 1
Share

Hi @cspecht

I don't think OnCollisionEnter or any similar method would be called twice, Destroy is run in the end of frame, so the gameobject is gone in next frame AFAI$$anonymous$$.

Despite of this, try doing this;

Add a field in your GemScript: "collected = false",

Then inside your if statement, add "collected = true;".

Then change your if to:

  if (collision.gameObject.tag.Equals("Player") && collected == false)
avatar image cspecht eses · Sep 27, 2018 at 02:09 PM 1
Share

Thanks @eses

Yes, that has worked! Nice. Just an extra double-check to ensure it's not doing the collision detection twice - which it is. . .

Looked a little further and it became obvious: my Player has a box AND a circle collider (I use a circle collider for the feet to smooth ramp movement). Obviously both colliders were sometimes triggering the GemScript.

You're awesome.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

153 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Platformer Character Acts Weird When Collide Side of a Platform 0 Answers

Is there a way for my to put 2 box colliders on the Same object but only have one of them check the trigger? 0 Answers

Increase the number just when the button is pressed 0 Answers

Score on collision only updates once 1 Answer

Collision detection between target and sphere not working (3d),Collision between target and sphere not working (3D) 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