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 /
avatar image
0
Question by EmerickGrimm · Feb 22, 2019 at 06:01 PM · triggertextfloat

Collider trigger two times, even if it's not active.

I have simple coin counter, in 2d Unity game. But It's count one coin as 2. If we look in to logs, we cant see that they work twice, when it's even not enabled.

     public GameObject coin;
     public Text CoinCounter;
     private int TotalCounter = 0;
     public BoxCollider2D collider;
    
 
     // Start is called before the first frame update
     void Start()
     {
         int.TryParse(CoinCounter.text, out TotalCounter);
         Debug.Log("Counter on Start :" + TotalCounter);
         Debug.Log("Text o");
 
     }
 
     private void Update()
     {
            TotalCounter = Convert.ToInt32((CoinCounter.text));
      }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         collider.isTrigger = false;
 
         if ((collider.isTrigger) == false)
         {
             Debug.Log("Collider Is not Trigger");
         }else
         {
             Debug.Log("Collider is still trigger");
         }
       
     
         Debug.Log("Trigger Entered");
 
         coin.SetActive(false);
 
         Debug.Log("Object is not active ");
         
         TotalCounter += 1;
         Debug.Log("Total Counter + :" + TotalCounter);
         CoinCounter.text = TotalCounter.ToString();
         Debug.Log("Text after +:" + CoinCounter.text);
    
     }
 }
 ![alt text][1]


[1]: /storage/temp/133581-logs.png

logs.png (59.4 kB)
Comment
Add comment
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

1 Reply

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

Answer by AaronBacon · Feb 22, 2019 at 06:36 PM

The Unity OnTriggerEnter2D can be a bit buggy and occasionally run twice, though you may want to check you haven't put multiple trigger colliders on either the player or the coin, as it will be running for each collision. A simple Workaround if that's not it would be to check if the coin is enabled before disabling it and adding to the coin counter. Eg:

  if (coin.activeSelf)
      {
          coin.SetActive(false);
  
          Debug.Log("Object is not active ");
          
          TotalCounter += 1;
          Debug.Log("Total Counter + :" + TotalCounter);
          CoinCounter.text = TotalCounter.ToString();
          Debug.Log("Text after +:" + CoinCounter.text);
      }
Comment
Add comment · Show 1 · 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 AaronBacon · Feb 22, 2019 at 06:42 PM 0
Share

Another thing I would say is you may want to use Tags in the editor to make sure the player can differentiate between coins and other trigger objects. Assign the "Coin" tag to the coin, then do something like:

      private void OnTriggerEnter2D(Collider2D collision)
      {
        if (collision.gameObject.CompareTag("Coin"))
      {
      // Run Code if its a coin
      }
 

then make the coin object a Prefab by dragging it into the file explorer in the editor, so you can simply drag a completed coin object into the scene.

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

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

Coin counter using public float losing score over time? 1 Answer

I need some help Stopping a timer when the player touches a trigger. I've got two scripts. One for Timer and other for the trigger. 2 Answers

i am new at unity, how to add location in float values in gui ? in following i want to add location of ** x (25.78) and y (25.98) ** but there is an error, 1 Answer

Execute on one frame using a float - Been looking for a solution for a while now... 1 Answer

Trigger FinishLine 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