I am new to Unity, and I have run into a problem that I cannot find the fix to.
The code below logs the colliders that are colliding with the BoxCollider2D of the character on the top right, but PF Player (the one on the left) is being logged (as seen in the screenshot), despite its box collider being far off from where the collider of the top right character is. (This only happens after I enter the BoxCollider2D of the right character, and before I enter it does not show that they are colliding.)
I have an exact copy of the character (the one that is being highlighted in the screenshot), but this one does not exhibit the same behaviors.
(As you can see in the screenshot, the console logs PF Player, even though the PF Player collider is not colliding with the collider of the character on the top right)
Code that I use to detect that they are colliding:
You’re going to have to simplify it down to find out what’s going on. Maybe there are other colliders. Maybe your script is on more objects than you realize. Maybe something else is getting in the way?
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
I found the answer after I posted the question, and the problem stemmed from my misunderstanding of the OverlapCollider method; I thought the method would override the hits array from before, but instead it kept them in the array, which was causing the problem.
The fix was to get the number of overlapping colliders returned by the OverlapCollider method, and iterate through the hits array with that number as the upper limit (ex.
for (int i =0; i < numberReturnedFromOverlapCollider; i++) {
// iterate through array here
}
Yes, the idea is you reuse it. Clearing the array is CPU time wasted that you can better use in your game so it’s not done. Also note that for all 2D physics queries, you can also provide a List for results which is even better because it’ll also automatically manage the capacity for you so you won’t ever loose results (if the array is too small).