Why is the code not called?

Hiya. I am doing my first ever unity-project, and I find it excruciatingly much more trouble than Visual Studio or Flash ever was. Enough about that, forward to the problem at hand.

My FPS character that I made myself because the built in one did not suit my purpose walks into a cylinder. This cylinder is in fact a portal. I do not ask for being able to see through it, I just want to transport. Right now, I am happy if ANYTHING can get through it.

I attached the following code to the portal.

function OnTriggerEnter (other : Collider) {    
    Debug.Log("Somethin'");
    if (other.CompareTag ("Player")) { 
           other.transform.position = Vector3(0,0,0);
    }     
}

And quite plainly, it does not do anything. I can stand on the portal, I can blast it, I can jump into it, but I do not arrive at position 0, 0, 0. It does not even write "Somethin'". The code is clearly not called upon.

Many of you probably silently thought about how stupid and easy a mistake I have made. Please tell me what it is.

Thank you. Sincerely, PlatinumSkink

Does your FPS character have a Rigidbody or a CharacterController component? Without either of those, OnTriggerEnter can never fire.

  • It could be that you have not the script on the same object that has your collider, on the portal.
  • Or your player might lack a collider.
  • Or your players collider is a trigger.
  • Or your collision masks could be wrong.

If you create a box or something and attach a rigid body to it and let it fall into the portal, does it trigger the "Somethin'" message? If it does, then it sounds likely that either:

  • Your player have no collider.
  • Your player have inappropriate collision masks.

If it doesn't register the message even with the rigidbody, then your object might not have the script and trigger attached on the same object.

I have answered a similar question that might add more insight. I felt this was related so I crosspost.