Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

OnCollisionExit2D issue

Discussion in 'Scripting' started by MTyler87, Jun 18, 2018.

  1. MTyler87

    MTyler87

    Joined:
    May 20, 2015
    Posts:
    19
    Hi there, I was wondering if anyone had a solution to my problem,
    I'm basically trying to store colliding object data in a var through OnCollisionEnter2D and clear it from the variable it through OnCollisionExit2D..
    Also targeted is a public GameObject var if that matters?

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D coll) {
    2.         if (coll.gameObject.tag == "Land")
    3.         {
    4.             targeted = coll.gameObject;
    5.         }
    6.     }
    7.  
    8.     void OnCollisionExit2D(Collision2D coll)
    9.         {
    10.             targeted = null;
    11.         }
    without OnCollisionExit2D segment the code works fine changing the targeted var to what it's colliding with but with OnCollisionExit2D it after the first target it skips objects right next to it and just plays up.

    I'm not sure I'm making sense but when colliding with an object it won't set my targeted variable with the object data it's colliding with.

    I have 5 squares next to eachother, the first one's object collision data will store in the 'targeted' variable just fine but the ones beyond that will not. I'm not quite sure where I'm going wrong.
     

    Attached Files:

  2. jackmememe

    jackmememe

    Joined:
    Jan 21, 2016
    Posts:
    138
    Not sure, but it may be calling the OnCollisionEnter2D on the second block before calling OnCollisionExit2D on the first one. In this case you will be NULL the second target.
     
    MTyler87 likes this.
  3. MTyler87

    MTyler87

    Joined:
    May 20, 2015
    Posts:
    19
    Thanks for the suggestion, I have tried moving the OnCollisionExit2D above the Enter to call it first but it hasn't made a difference if you mean to try that?

    I get these errors on runtime if it helps, I don't think they matter though, just complaining because they're empty vars I've taken it as.
     

    Attached Files:

    Last edited: Jun 18, 2018
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    No, these are event handlers. They are called when the linked event is triggered. That is the point that jackmememe is making. The ordering of the methods within the file is irrelevant. :)

    Can I ask, are you are using the debugger at all? It is an incredibly powerful tool and extremely useful. If you were to breakpoint inside the 2 event handlers (OnCollisionEnter2D and OnCollisionExit2D), it would quite nicely highlight what is happening.

    However, you may want to consider storing the collisions as a list to maintain a record of currently active targets.
     
    MTyler87 likes this.
  5. MTyler87

    MTyler87

    Joined:
    May 20, 2015
    Posts:
    19
    I had a try yesterday but I was struggling a bit using it (the debug), I'll need to look it up and learn more about it now I've got access to the internet again.
    I've uploaded my project incase that makes it easier to understand what I'm trying to do? If someone has time to have a look it would be much appreciated, I'm quite dumbfounded.

    EDIT: Is it just not possible to set and empty a target var accurately using the collision enter/exit functions?
     

    Attached Files:

    Last edited: Jun 19, 2018
  6. MTyler87

    MTyler87

    Joined:
    May 20, 2015
    Posts:
    19
    Hurrar I finally found myself a solution, will post for future reference incase someone else comes across the same problem..

    Code (CSharp):
    1.    
    2. public class Target : MonoBehaviour {
    3.     public GameObject targeted;
    4.     public GameObject targeted_wiped;
    5.  
    6. void OnCollisionEnter2D(Collision2D coll) {
    7.         if (coll.gameObject.tag == "Land")
    8.         {
    9.             targeted = coll.gameObject;
    10.         }
    11.     }
    12.  
    13.     void OnCollisionExit2D(Collision2D coll2)
    14.     {
    15.         targeted_wiped = coll2.gameObject;
    16.         if (targeted == targeted_wiped)
    17.         { targeted = null; }
    18.         }
    19.  
    Hopefully this won't cause problems in the future but it's working so I'm very pleased, been stuck on that for days trying to understand why it wouldn't work.
    Thank you for the help provided, it did help me understand it more.
     
    jkhenfer and Doug_B like this.