I was working on the game as usual, and hit the build button on Visual Studio to compile the code. Then I went back in to Unity, and I discovered that the enemy characters wouldn’t do damage to my player any more. I was quite surprised because I didn’t made any changes to the relevant code.
After some debugging, I managed to narrow down the problem. But before, let me tell you how the enemies do damage to the player. The enemies carry a weapon game object (like a sword), that has a box collider. The box collider acts as an actual collider in general, (because I want the gameObject to react to physics) but when the enemy is attacking it is turned to a trigger. So when the weapon trigger touches the player’s character controller (which has a collider of each own), the player looses some hit points.
So here is what I discovered through debugging. I have a piece of code in the Attack() method:
myCollider.isTrigger = true; commons.IsAttacking = true;
One turns the weapons collider to a trigger, and the other lets the game know that the enemy is attacking, so the player doesn’t loose health if he just touches the weapon when the enemy is not attacking. I used Visual Studio step-by-step debugging, and both these lines of code seem to execute without a problem. But neither of these lines are actually executed. When I go back into the Unity editor, the collider of the weapon is always NOT a trigger, and when I check the value of isAttacking, it is always false.
How is this even possible ?