OnTriggerExit not working SOLVED

Sorry I know I just posted this but I found the answer, for those looking for the same solution I needed to add a collider to my main character instead of just using the character controller… that was my mistake.

Hi all, I have been racking my brain for the last couple days trying to figure this out on my own but even after looking at example scenes and copying what their doing I still can;t get the OnTriggerExit function to work… here is the setup.

I have a bunch of prefab objects. I want it so when my character walks up to one of the objects, that object’s OnTriggerEnter turns on and when the character walks away from it it turns off.

I have the OnTriggerEnter working and firing but the OnTriggerExit does nothing and does not fire.

The objects have a sphere collider with “is trigger” checked and they have a rigid body attached as well with gravity turned off and iskinematic turned on, collision is discrete.

The character has a character controller attached as welll as several scripts and a rigid body as well with gravity unchecked and iskinematic unchecked and collision is set to discrete.

Here is the code for the static objects or the piece that is relevant anyway.

function OnTriggerEnter(collider:Collider){

	var playerStatus : CharacterStatus = collider.GetComponent(CharacterStatus);
	if(playerStatus == null) return;
	
	if(pickedUp) return;
	Debug.Log ("Looking at object");
	ShowRing();

	//everything's good, so put it in the inventory
	
	var characterInventory = collider.GetComponent(CharacterInventory);
	characterInventory.GetItem(itemType, itemAmount);
	
	pickedUp = true;
	//Destroy(gameObject);
	
}

function OnTriggerExit(collider:Collider){
		//var playerStatus : CharacterStatus = collider.GetComponent(CharacterStatus);
			//if(playerStatus == null) return;
			
			Debug.Log ("object untouched");
				
			HideRing();
}

what am I doing wrong? :frowning:

Cheers,
Jason

Hi!!

what you are saying is that I must remove my Character Controller and use a simple Capsule Collider?

So, there is no problem with the gravity and the movement? I’m calculating the gravity by my own like in the example of the ThirdPerson Controller and I’m using a the path that returns a PathFinding to get move. So, this didn’t bring you more problems than the CharacterController??

If you are using two collider on the same game object ontriggerexit will never be called. For example if you have on the same game object a character controller and a sphere collider with trigger on ontriggerexit will never be called because it is colliding with the character controller. You can detect collision using ontriggerstay otherwise

A good way to do this is to put two private boolean global variable one called stay and the other called iscolliding, than into the OnTriggerStay function put this code:-
function OnTriggerStay(col : Collider){
if(col.gameObject.name==“The name of the gameobjet i want to know if i collide”){
stay=true;
}
// so if i’m colliding with myself it will not be reported
}

And into Update function put this code :-

if (stay){
iscolliding=true;
stay=false;
} else iscolliding=false;

than test only iscolliding variable…
:slight_smile:

Hi , Its works for me now in temporary , but cant we have some other solution as we are checking its in update continuous .

I know that you wont read this, but to be more precise you can use UpdateFixed instead of Update
I tested and its working

FixedUpdate instead of UpdateFixed :wink:

1 Like

I´m on the same problem. I don´t understand what the original post meant by the following:
“I needed to add a collider to my main character instead of just using the character controller”
Both my characters have colliders, and I still dont get on trigger exit to work

Since this is a top result on Google I wanted to link to a more robust solution

ended up here from google also (ontriggerexit not called when collider is disabled) , my workaround was:

changed my void Die() method into IEnumerator Die(),
then move the transform away for 1 frame, before disabling collider on it:

var oldpos= col.transform.position;
col.transform.position = new Vector3(9001, 0, 0);
yield return null;
col.enabled = false;
col.transform.position = oldpos;

I’m still confused on how you did this?