[SOLVED] OnTriggerEnter not working correctly.
So I know this question has been asked a lot, and I've looked at the other answers as well, but I still can't fix my problem. What I have is a moving cube, with a rigidbody and a box collider, and another cube that has a box collider that isTrigger. I have this code attached to the rigidbody cube:
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.Translate (-1.65f, 0, 0);
}
void onTriggerEnter(Collider coll){
Debug.Log ("collided");
}
The rigidbody cube travels through the trigger cube. My problem is the Debug.Log is never called. What am I doing wrong?
SOLUTION: Make sure OnTriggerEnter is capitalized. Always check your methods for accuracy
Answer by ElijahShadbolt · Nov 25, 2016 at 10:27 PM
You are using Transform.Translate() instead of Rigidbody.MovePosition(). Since you are directly changing the position of the Transform without going through the Rigidbody, it thinks it is not moving, so no events are called.
void Update()
{
GetComponent<Rigidbody>().MovePosition(transform.position + new Vector3(-1.65f,0,0));
}
I put in your code and it is still not returning anything.
Oh, it appears that you have written the function wrong. It should be OnTriggerEnter
not onTriggerEnter
.
And after fixing that, I tested it out with regular old transform.Translate()
and that works too.
It works! Thank you so much! You would think that a simple capitalization issue would be caught while building though haha
(actually OnTriggerEnter, Update, Awake, Start, etc are just normal functions. Unity uses Send$$anonymous$$essage to invoke specific functions when you have included them in your script. This means that different spelling would not result in an error, because you could very well have another function with a similar name.)