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 ExtraFlash8 · Apr 13, 2017 at 02:57 PM · c#triggercollider2dcollision2d

detect a trigger one time per collision

In my game there are 2 players, Player1-a and Player2, when the players collide, the text on each one of them is changing (Player1-a is Player1 and Player2 is Player2-a). The problem is that it keeps changing texts while the players are colliding (it can switch the texts 2 times for one collision) and i want it to change the texts only one time per collision. And I also need that after a collision it wont be able change texts for 5 seconds because if they will collide again after 1 second it will change the texts again). This is the player1 collision action:

  void OnTriggerEnter2D(Collider2D other)
      {
          if (other.CompareTag("TriggerOn"))
          {
              if (firstPlayerText.text == "Player1-a")
              {
                  firstPlayerText.text = "Player1";
                  secondsPlayerText.text = "Player2-a";
              }
          }
      }

and this is the collision action of the other player:

  void OnTriggerEnter2D(Collider2D other)
      {
          if (other.CompareTag("TriggerOn2"))
          {
              if (secondsPlayerText.text == "Player2-a")
              {
                  firstPlayerText.text = "Player1-a";
                  secondsPlayerText.text = "Player2";
              }
          }
      }

By the way I also want that after the collision, the player2 with the new text (player2-a) wont be able to move for 5 seconds.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Apr 14, 2017 at 10:03 AM

When two objects collide with each other, both receive the collision information.

So, the best practice is to have only one of those patterns :

  • object minds its own business, does things to itself but leaves the other one alone

  • object does things to the other object, but does nothing to itself

Or you can also use if (gameObject.GetInstanceID() < other.gameObject.GetInstanceID()) to do things only on one of the two by comparing their InstanceID which will always be unique. As GetInstanceID() has a cost, you can also do it only when (gameObject.tag == other.gameObject.tag). Like :

 if (gameObject.tag != other.gameObject.tag || gameObject.GetInstanceID() < other.gameObject.GetInstanceID())
 {
     // we're different or I was first, I handle collision
 }
Comment
Add comment · 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
0

Answer by ExtraFlash8 · Apr 14, 2017 at 02:01 PM

@UnityCoach Thanks a lot for the answer

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers

How to check if player collides with trigger? 0 Answers

Constant Collision 1 Answer

Collision with trigger is not working 1 Answer

2D Game - Get collider name 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