- Home /
Collider not functioning correctly
When I use the :
void onTriggerEnter(Collider collider){
if (collider.gameObject.CompareTag("Enemy")){
Player.score += 1;
}
code the Player.score isn't being increased by one.
Also when:
void OnTriggerEnter(Collider collider){
if (collider.gameObject.CompareTag("Projectile")){
DestroyObject(this.gameObject);
if (collider.gameObject.CompareTag("Player")){
Destroy(this.gameObject);
}
}
Is processed the object isn't being destroyed when the colliders' tag is "Player" How to fix
I am using C#. Both the enemy and the projectile are moving.
One of the object has to have a non-kinematic Rigidbody, and both have to have colliders. If you put a this:
Debug.Log(collider.tag+", "+collider.name);
...just inside the OnTriggerEnter(), does if fire?
Try collider.gameObject.tag == "Enemy" ins$$anonymous$$d of "compareTag".
Answer by MrYOLO · Feb 27, 2014 at 09:35 AM
Try to put the rigidbody on the moving object, then it will trigger the OnTriggerEnter function when it collide with an collider. I'm not clear of your struction(which has the rigidbody and which is moving, also the script is bundled for which gameobject). But the OnTrigerEnter is only called when one of the gameObject has rigidbody and the moving one is the one with rigidbody. It's seems if the one with rigidbody is not the moving one, it will sleep at the processing, I think it a way unity use to optimize the Engine. Good Luck.
Your answer

Follow this Question
Related Questions
GUI text not updateing 1 Answer
Scoring system based on time remaining and targets hit 4 Answers
Score not adding up? 1 Answer
how can i keep the variable show in play mode 0 Answers
How do I stop adding to a variable? 1 Answer