- Home /
Player health script doesnt work?
Yo. I made a player health script and an enemy bullet script... I have an enemy script and a normal bullet script too and there the damage works perfectly. But for whatever reason the nearly exact same scripts are not working on the enemyBullet and the player scripts, so what i mean is that the player doesnt get damage although he has the proper script... Can somebody help and tell me what is wrong? There are two screenshots attached with the enemyBullet and the player script ;)
$$anonymous$$ake sure you have set the correct value for the damage variable in enemyBullet in inspector or set it in runtime if it is instantiated. I suggest you add a default damage value in the code using public int damage = 1;
.
Answer by mudassirrazi · Feb 03, 2020 at 04:17 PM
Have you set the damage value in the EnemyBullet script in the editor?
Answer by unity_ek98vnTRplGj8Q · Feb 03, 2020 at 07:06 PM
Careful, you have two different "player" variables in your bullet script, this is never a good idea. Also make sure that your collision layers are correct and that your bullets are actually detecting the collisions. I suggest adding a Debug.Log(hitInfo.gameObject.name) just to make sure the collision is actually being triggered. If its not then likely either your collision layers are off (check under the physics settings to see the collision matrix), or your bullets are traveling too fast to trigger a collision (raycasting between bullet positions is a common way to fix this issue for fast-moving projectiles)
so i tested it (you can see script changes in the screenshots that are attached) and it didnt even showed the name of the player in the console.. $$anonymous$$y player has a collider and the EnemyBullet too. I dont know why it doesnt show that i hit the player :(
to clarify, is your player collider a trigger? Using the OnTriggerEnter function will only work if you have the "trigger" box checked on the collider (I think)
Jesus im stupid xD I fixed it with checking the trigger box and adding a fire point to the enemy (the point where his projectiles shoot from) and now it works perfectly fine :) Thanks for helping dude :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612222645im_/https://answers.unity.com/themes/thub/images/avi.jpg)