OnCollisionEnter suddenly stopped working
This is by far the weirdest thing. I implemented an OnCollisionEnter hit detection with my enemy and it worked great at first in the editor, as soon as I built and ran the game it stopped working in not only the build but the editor as well!
My enemy script has this:
function OnCollisionEnter (Col : Collision) {
if (Col.gameObject.tag == "HitBox0") {
var player = GameObject.FindWithTag("Player"); //Find the player
player.GetComponent(PlayerControl).OnAttack();
print ("Hit detected!");
}
}
function Hit () {
CurEnemyHealth -= knfdmg;
}
My player has this which initiates the "hit" when PlayerIsAttacking = true, in which when I attack it sets that variable to true.
//Attacking enemy
function OnAttack () {
if (PlayerIsAttacking == true || KnifeAttkFwd == true || KnifeAttkUp == true || KnifeAttkDown == true) {
var enemy = GameObject.FindWithTag("Enemy"); //Find the enemy
enemy.GetComponent(ZombieAI).Hit();
}
}
I don't get why it worked perfectly the first time then just decides to stop working after I built the game?
The HitBox object has a mesh collider and a rigidbody. All of the tags and scripts are set to their respective objects so what gives?
Answer by VirusPunk · Aug 29, 2015 at 09:53 PM
I sort of figured out what the problem was. It had something to do with the character controller attached to the enemy AI model. Every time you close and reopen Unity or build the project I have to remake the character controller for some weird reason. I guess it's just a weird bug as a result of using an older version of Unity, maybe.