- Home /
how do i make an Enemy take damage from prefab bullet clone?
i have the code for ray casting working well but im wanting to shoot a laser thatspawns a laster object that hits an enemy and it does damage to the enemy. i have gotten destroygameobject to work but not to take away health (i have it working with raycast it takes away health)
for some reason when the laser collides with the enemy the laser destroy object works but the enemy is not taking damage i have tried to get component like i did with ray cast as well as made a collision on the enemy itself but i cant get it to take damage.
When the left click is pressed it spawns the bullet prefab when the prefab spawns in the H it is named Bullet(Clone) i have also given it the tag laser nether has worked.
i have tried
void OnCollisionEnter(Collision col) { if (col.gameObject.name == "Bullet") { health -= 50f; } } void OnCollisionEnter(Collision col) { if (col.gameObject.name == "Laser") { health -= 50f; } } what am i doing wrong
Answer by PersianKiller · Jan 07, 2018 at 04:54 AM
you should use col.gameObject.tag == "Bullet" .because instantiated clones don't have the same name :)
so i should use a tag. just making sure i should put the prefab tag as "bullet" or "laser" and that will make all the bullets that come out tagged the same?
yeap.the bullet and laser should have a tag.name it what ever you want,because if you instaniate them.the tag won't change but the name will be. for example name of the first one is Bullet,and the second is Bullet(1) and ....
ok thanks i will give it a try just wondering should i put the collision on the laser or the enemy which is better practice
sry .I don't know I haven't worked on 3D projects :).(only 2D )
Your answer
Follow this Question
Related Questions
Destroy object on touch of object with specific class 3 Answers
Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers
Targetting Script Help 1 Answer
Attacking more than one enemy with same tag, but unity only allows one enemy at a time? 2 Answers
Object moving too fast, so that the collider does not work (C#) 2 Answers