• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

OnTriggerEnter2D acting like OnTriggerStay2D 0 Answers

Location of RigidBody2D on collision 3 Answers

How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer

How do I detect which collider is being collided with? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges