Bools not switching off once collider is destroyed while in players colliders range

Hi, I have a problem again lol.

For some reason unknown, my bools wont switch back off after I destroy a block.

I have the player set with a collider on each side to test if there’s a wall there, if so then I switch a bool on which disables the player from being able to keep walking towards the wall which stops it from getting stuck.

I also have destroyable walls and when standing next to a wall and destroying it, if its within the range of the colliders the bools wont switch off again until I climb onto a ladder or something else. My code for switching the bools on and off are as follows:

void OnTriggerStay2D (Collider2D other)
		{
				Collider2D collider = other.collider2D;
		
				if (collider != null) {
						string tag = other.collider2D.tag;
			
						if (tag == "PLATFORM" || tag == "SOLID") {

								if (Player_Control.onLadder == false) {
										leftContact = true;
										

								} else {
										leftContact = false;
									
								}
						} else {
								leftContact = false;
								
						}
				} else {
						leftContact = false;
					
			      }
				
    		}
  • and to make sure it switches off:

    void OnTriggerExit2D (Collider2D other)
    {

     			leftContact = false;
     			
     	}
    

Then all I have on the player is:

if ((lr > 0 && isRightSideContact.rightContact == false)) {//
								walk = 1;
						} 
  • With lr being the horizontal axis var.

So once I destroy a block while standing next to it the bool doesn’t switch back off. It works well otherwise. Could someone maybe point me in the right direction as to whats happening here??

Your code is quite hard to follow, and wrong in places. The other parameter passed to OnTriggerStay2D is a collider2D, so you don’t need to try to access its collider2D as other.collider2D. Likewise, it can’t be null, because if the colliding object had no collider, OnTriggerStay2D wouldn’t have been called in the first place. And your if/else logic is a bit inefficient.

I would rewrite it like this:

void OnTriggerStay2D (Collider2D other)
{
  string tag = other.gameObject.tag;

  if ((tag == "PLATFORM" || tag == "SOLID") && !Player_Control.onLadder) {
    leftContact = true;
  } else {
    leftContact = false;
  }
}

If that does’t work, add Debug.Logs to see whether the function gets called at all and, if it does, what the tag of the triggering object is.