- Home /
Bullet and Enemy can't collide and execute functions
I've already tagged the gameObjects which is the bullet and enemy with the correct names. They have their own colliders as well with rigidbody too however the bullets will not execute the function to destroy the enemy gameobject and give me the score that I should get.
what is attached to the bullet prefab
using UnityEngine; using System.Collections;
public class bulletCollide : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision){
if (collision.gameObject.tag == "enemy"){
spawnEnemy cs = collision.gameObject.GetComponent<spawnEnemy>();
}
}
}
what is attacked to the enemy prefab:
public class spawnEnemy : MonoBehaviour { public Transform enemy = null; public int health = 8;
void OnEnterCollision(Collision collision) {
if (collision.gameObject.tag == "bullet") {
health --;
if (health <= 0) {
HUD hud = GameObject.Find ("Main Camera").GetComponent<HUD> ();
hud.hitEnemy ();
Destroy (gameObject);
}
}
}
}
I've tried using Collider ins$$anonymous$$d, but it still occur the same problem. Thanks for helping though :)
Answer by SepM · Jul 11, 2014 at 02:04 PM
This is a very common issue in Unity. (Kinda wish they'd make it easier to figure out... ^_-')
I've run into the same problem. Instead of Collision, I use Collider.
void OnEnterCollision(Collider collision) {
Also, you can try Rigidbody in place of Collision, but I actually prefer triggers. In my JavaScript, I have:
function OnTriggerEnter(collision : Collider){
if(collision.gameObject.tag == "Player"){
}
This is a really annoying hurdle to get over for most/all of us. I hope you get beyond it!
Your answer
![](https://koobas.hobune.stream/wayback/20220613153807im_/https://answers.unity.com/themes/thub/images/avi.jpg)