Inconsistent variable value with OnCollisionEnter and OnCollisionExit

Hi, I have this script :

private int slideBank;

void Awake () {
         slideBank = 0;
}

void Update() {
          Debug.Log(slideBank);
}

void OnCollisionEnter(Collision hit) {
        if (hit.transform.CompareTag ("Slide")) {
            {
                Debug.Log("IN");
                slideBank++;
            }
        }
}

void OnCollisionExit(Collision hit) {
        if (hit.transform.CompareTag ("Slide")) {
            {
                Debug.Log("OUT");
                slideBank--;
            }
        }
    }

So the problem is, when my player (which is own this script), and another object that have “Slide” tag colliding, my console only showing “IN” once and without “OUT”.

But inside the Update debug, it shown
1 0 1 0 1 0 1 0 1 0 1 0 1 0
while the object still collide.

Notice that we have zero in there while I only decreased the value if “OUT” is shown.

Do you have any idea why does it happen?
Or maybe there’s something about OnCollissionEnter / Exit that I missed.

Thanks.

Think it’s possible that you have 2 of these scripts in your scene, maybe?

1 Like

Oh my ._.

You were right, I feel silly. Thanks for catching that.

no problem. happens somewhat often :slight_smile: