Something happened. What's wrong ?

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 ?

The code looks fine.

You should show us all the code…

How about putting them into Coroutine?

Or you can use Animator’s SetBool.

      //Player health hit by enemy


Something like this

Problem solved.
As I initially stated, it wasn’t a mistake in the code, evidenced by the fact that recovering back the older working version of the code with git didn’t fixed the problem. It was a problem with names of gameObjects in the scene. The sword carried by that enemy wasn’t the only one carrying its name. There was another sword somewhere in the scene, with the exact same name. So the sword that its collider was becoming a trigger was another than the one the NPC was carrying and attacking with. That’s what can happen when it is allowed for different objects to have the same name in the same environment.