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 /
avatar image
0
Question by Borna_ · Jan 31, 2018 at 01:44 AM · collisioncollidercollider2doncollisionenteroncollisionstay

OnCollisionStay2D is ignoring OnCollisionEnter2D

I have a round player with Circle Collider 2D and obstacles with Edge Collider 2D. When player touches one obstacle, player changes color and keeps changing colors and expanding until player moves away from obstacle, but if the player touches the second obstacle while still touching the first, the game is restarted. I have a problem with detecting second obstacle. Sometimes second obstacle is detected and my OnCollisionEnter2D works fine, but most of the time it doesn't. I feel like program is doing only OnCollisionStay2D and it is ignoring OnCollisionEnter2D so in the meanwhile player touches one more obstacle while still touching the first but the game is not restarted. It feels like it is doing OnCollisionStay2D forever. Collision detectiod: Continous

 void OnCollisionEnter2D (Collision2D other){
         if (other.gameObject.tag == "Obstacles" && wasDetected == false) 
         {
             wasDetected = true;
             Debug.Log (other.gameObject.name);
         } 
         else if (other.gameObject.tag == "Obstacles" && wasDetected == true) 
         {
             wasDetected = false;
             Debug.Log (other.gameObject.name);
             theGameManager.RestartGame();
         }
     }
 
     void OnCollisionExit2D (Collision2D other){
         wasDetected = false;
     }
 
     void OnCollisionStay2D (Collision2D other){
         if (other.gameObject.tag == "Obstacles") 
         {
             transform.localScale = new Vector3 (transform.localScale.x + 0.1f, transform.localScale.y + 0.1f, transform.localScale.z);
             myColor.color = new Color(myColor.color.r + 0.05f, 0, 0);
         }
     }
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 Bale_txy · Jan 31, 2018 at 02:30 AM

try moving all codes inside OnCollisionEnter to OnCollisionStay, that should help

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 Borna_ · Feb 01, 2018 at 02:05 AM 0
Share

If I move all the code form OnCollisionEnter to OnCollisionStay then player will die at the very beginning of the game. I forgot to mention that most of the time player collides with the obstacle but OnCollisionEnter2D is not called.

avatar image
0

Answer by melsy · Feb 01, 2018 at 06:18 AM

is there a rigidbody2d on the player?

Comment
Add comment · Show 3 · 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 Borna_ · Feb 01, 2018 at 06:57 PM 0
Share

Yes, there is.

avatar image melsy · Feb 02, 2018 at 12:55 AM 0
Share

You need to put a tag if on the exit as well. Otherwise any exit at all will trigger that. The easiest way to find the problem is to put a Debug.Log(stay,enter,exit) in the callbacks. That way you can see where it is malfunctioning

avatar image Borna_ melsy · Feb 02, 2018 at 10:45 PM 0
Share

There are just obstacles is my game, but i also tried to put if statement and it didn't solve the problem.

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

128 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

Related Questions

OnTriggerEnter2D works without being actually triggered. 0 Answers

I am trying to create all elements in the scene from one script at run time. How do I detect collisions between the enemy and the coin without having to create another script for each item? 0 Answers

Detecting empty collider 1 Answer

OnCollisionEnter 1 Answer

Unity3d Arkanoid platform collision problem 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