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 niamut · Dec 29, 2015 at 11:13 AM · 2d gameplayerboxcollider2dpointontriggerenter2d

How to disable a boxcollider2d temporary?

hi guys,

So i'm making a game similair to flappyBird and whenever you trough the pipes you get a point. The problem is sometimes my player hits the box twice. it gets two piont instead of one. So i want to disable my boxcollder2d for a second after it gets hit by my player. The boxcollider2d are in a loop so they can't be disabled forever.

code i used for my collider2d to trigger:

         void OnTriggerEnter2D(Collider2D collider) { 
         if(collider.tag=="Player"){
         PlaySound(0);
         Scores.AddPoint();
     }
 }
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 Gnorpelzwerg · Dec 29, 2015 at 11:39 AM 0
Share

get trigger collider with GetComponent( ). you can disable any component by setting Component.enabled to false.

avatar image niamut Gnorpelzwerg · Dec 29, 2015 at 12:04 PM 0
Share

thanks for the reply

this disables my boxcollider2D after it is triggerd. my boxcolliders2d are in a loop so this is not a option. can't i set a timer or something?

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by dreamhex · Dec 29, 2015 at 12:10 PM

You could try this: http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

So it's like:

 void OnTriggerEnter2D(Collider2D collider) { 
          if(collider.tag=="Player"){
          PlaySound(0);
          Scores.AddPoint();
          GetComponent<BoxCollider2D> ().enabled = false;
          StartCoroutine(EnableBox(1.0F));
      }
 
   IEnumerator EnableBox(float waitTime) {
         yield return new WaitForSeconds(waitTime);
         GetComponent<BoxCollider2D> ().enabled = true;
     }

I feel like there is a better option to handle this situation, maybe someone will tell you, but this should work too.

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 niamut · Dec 29, 2015 at 12:52 PM 0
Share

Thank i think this will work!

avatar image dreamhex niamut · Dec 29, 2015 at 01:15 PM 0
Share

Does it work? Or did you go with LazyElephant's solution? That should work too with OnTriggerExit2D function.

avatar image niamut dreamhex · Dec 29, 2015 at 01:24 PM 0
Share

Your solution worked . I dont know how to do it with the OnTriggerExit2D function.

Show more comments
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

6 People are following this question.

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

Related Questions

How to let player hit a boxcollider just once?(2D) 1 Answer

wanna find size datas from boxcollider2D in script 0 Answers

GameObject keeps colliding for sometime after being destroyed 1 Answer

System Equipment and Changing Clothes in 2d Endless Runner Game ? 0 Answers

i want random color in my five block but not same color how i do that 3 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