- Home /
function OnTriggerEnter doesn't work
Hi folks,
I wrote a Js for my character, but it doesn't work for me. Don't know what I did wrong. Maybe you can help me.
This is my Js;
var character : Transform;
var explosion : Transform;
function Start()
{
explosion.gameObject.SetActive(false);
}
function OnTriggerEnter(other : Collider){
if (other.gameObject.CompareTag("CharacterTag"))
{
character.gameObject.SetActive(false);
explosion.position = character.position;
explosion.rotation = character.rotation;
explosion.gameObject.SetActive(true);
}
}
I put this Js in an object and I tagged my character to "CharacterTag". But when my character moves to that object. It does nothing. It just walk trough of that object. I added a Box Collider for my character and for that object. It just didn't work.
Please someone who can help me with this problem?
Thanks in advance
I've only ever used if (other.gameObject.tag == "theTagYouWant")
another thing to check is that one of the two objects colliding needs a rigidbody as well as a collision box. $$anonymous$$ake sure is trigger is set on both of course.
Are you assigning the transform variables (character and explosion) through the inspector?
If he wasn't I think he would get a null reference error message right?
@Rick74, you are correct. There would be a null reference exception posted. I should have asked the following: Are you receiving any messages in the console?
How are you moving your characters? Character Controllers do not cause OnTriggerEnter(). If you are using a Character Controller, you'll need to use OnControllerColliderHit() on the same object as the Character Controller.
Answer by androids · Oct 01, 2013 at 12:25 PM
I give an answer on my own question for those who have the same problem. What I did was just a simple step. I added a RIGIDBODY to the character. And because I don't want the character fall down, I just disabled "Use Gravity". Hope this answer would help your problem.