Problem with OnCollisionExit

Hello all, I’m making a 2d puzzle game. Currently my movement works great with 1 exception. If i am at a corner and i move away from 1 of the two objects I’m touching. Then my oncollisionexit function is triggered and allows me to move through the other object if i never stopped touching it. So I think I need someway for it to check if I am still collided with my values before resetting them to null, but I’m not sure how to do that if you even can.

#pragma strict

var speed : int = 2;
private var collided_with : GameObject[];
var left : boolean = true;
var right : boolean = true;
var top : boolean = true;
var bot : boolean = true;
var hasCollided : boolean = false;
var impact : boolean = false;
var twoFalse : int = 0;

function Awake () {
	collided_with = new GameObject[2]; 
}

function Update () {
	

	 if(Input.GetAxis("Horizontal") > 0){ 		// this code makes the ship go right
	 	if(hasCollided == true  impact == false){
			if(collided_with[0].transform.position.x >= transform.position.x){
				right = false;
				impact = true;
				twoFalse++;
			}
		}
		if(collided_with[1] != null  twoFalse <= 2){
			if(collided_with[1].transform.position.x >= transform.position.x){
				right = false;
				twoFalse++;
			}
		}		
	 	if(right == true){
	 		transform.Translate(Vector3(1 * speed * Time.deltaTime, 0, 0));
	 	}
	 }
	

	if(Input.GetAxis("Horizontal") < 0){  		// this code makes the ship go left 
		if(hasCollided == true  impact == false){
			if(collided_with[0].transform.position.x <= transform.position.x){
				left = false;
				impact = true;
				twoFalse++;
			}
		}
		if(collided_with[1] != null  twoFalse <= 2){
			if(collided_with[1].transform.position.x <= transform.position.x){
				left = false;
				twoFalse++;
			}	
		}
		if(left == true){
			transform.Translate(Vector3(-1 * speed * Time.deltaTime, 0, 0));
		}
	}
		
	
	if(Input.GetAxis("Vertical") < 0){ 			// this code makes the ship go down	
		if(hasCollided == true  impact == false){
			if(collided_with[0].transform.position.y <= transform.position.y){
				bot = false;
				impact = true;
				twoFalse++;
			}
		}
		if(collided_with[1] != null  twoFalse <= 2){
			if(collided_with[1].transform.position.y <= transform.position.y){
				bot = false;
				twoFalse++;
			}
		}	
		if(bot == true){
			transform.Translate(Vector3(0, -1 * speed * Time.deltaTime,0));
		}
	}
	
	
	
	if(Input.GetAxis("Vertical") > 0){ 			// this code makes the ship go up	
		if(hasCollided == true  impact == false){
			if(collided_with[0].transform.position.y >= transform.position.y){
				top = false;
				impact = true;
				twoFalse++;
			}
		}			
		if(collided_with[1] != null  twoFalse <= 2){
			if(collided_with[1].transform.position.y >= transform.position.y){
				top = false;
				twoFalse++;
			}
		}		
		if(top == true){							
			transform.Translate(Vector3(0, 1 * speed * Time.deltaTime,0));
		}
	}
} // end of void update 
	
	
function OnCollisionEnter(col : Collision){ 
	if(collided_with[0] == null){
		collided_with[0] = col.gameObject;
		hasCollided = true;
	}
	else{
		collided_with[1] = col.gameObject;
	}
}
	
function OnCollisionExit(col : Collision){ // sets collided with to null after it hits something
	collided_with[0] = null;    [B] // This is the part im having a problem with, i need this to not get set to null if its still touching an object[/B]
	collided_with[1] = null;     [B] // but if i stop touching one of the objects they both get cleared[/B]
	hasCollided = false;
	if(left == false){
		left = true;
		twoFalse--;
	}
	if(right == false){
		right = true;
		twoFalse--;
	}
	if(top == false){
		top = true;
		twoFalse--;
	}
	if(bot == false){
		bot = true;
		twoFalse--;
	}
	impact = false;
}

Thanks for the help, and sorry if i have been posting a lot of questions today new to Unity.

I don’t exactly have the time to dig into the code right now, but maybe something like this would work:

if(col.gameObject == collided_with[0]) collided_with[0] = null;
else if(col.gameObject == collided_with[1]) collided_with[1] = null;

Ya i tried that already but it didn’t work. But thank you for the suggestion

Instead of using booleans to tell if you’re collided on each side, use integer counters. When you collide, add 1 to the relevant counter, and when you stop colliding subtract 1, and to see if you can move check whether or not the value is equal to zero.

Otherwise, if there are two collisions both sharing a boolean, when one ends the boolean gets set to false even though the other collision may be continuing.

Ok so if i did that would i make my oncollision exit be like this?:

if(col.gameObject == collided_with[0])  //    integer counter from collided_with[0] --;
else if(col.gameObject == collided_with[1])  //  integer counter from collided_with[1]--;

I’m working on trying what your suggesting and the problem i have is i don’t get how to make the integer counter for just the relevant collision go down when OnCollissionExit gets called.

Also when i tried playing around with integers it caused my twoFalse variable to act funny and caused some other problems i didn’t have with the old one, so if it is possible i would like to keep it the way it is with boolean values.

I used this here… seemed to have helped me…

public int colliderCount;

void Update()
{
 
if (colliderCount == 0)
{ 
print("No longer in contact with Anything!");
//DO STUFF!
}
}

void OnCollisionEnter()
{
colliderCount += 1;
}

void OnCollisionExit()
{
colliderCount -= 1;
}