- Home /
Destroy On Collision will not work with spawned AI
What's happening is when I fire my prefab gun, it doesn't destroy the "enemy"s, unless I use this script:
function OnCollisionEnter(collision : Collision) {
Destroy(collision.gameObject);
}
I use this script on the prefab bullets and I can manage to destroy the enemy(clone)s. However, sometimes this results in a ricocheted bullet hitting the terrain and causing the world to disappear. I tried to use this script:
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "Bullet")
Destroy(gameObject);
}
However, this failed to work, I checked and made sure each model has a rigidbody, the bullet, the first person controller, the enemy, and I checked and made sure none were checked as triggers, and each model has their own collider. Can someone help explain why is this script not working?
I'd like to make it so that the first person controller can be destroyed when the enemy collides with it as well, is this possible?
Thanks!
Answer by YoungDeveloper · Oct 11, 2013 at 10:05 PM
Are your collision checking script is located on bullet or enemy, and did you actually add a new tag "bullet" on bullet ?
The collision script is located on the enemy(so if the bullet hits it(or its clones)it will destroy it). I did not actually add a new tag "Bullet" on Bullet, I didn't know this was necessary, how do I do this exactly, do I just write Bullet in a new user layer?
Thank you very much, the tagging fixed my issue, thanks!
You're welcome, remember to mark the question as answered.
Your answer
Follow this Question
Related Questions
How to make collision check if other collider has component 1 Answer
Find colliders after collision? 2 Answers
Trigger Enter and Exit not working properly? 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Generating A Custom Collider For Multiple Meshes - Script 2 Answers