I found the solution
I want to ensure only 1 collision happens
So when I shoot my bullets and an enemy it sometimes causes 2, or even 3 collisions to happen before the bullet gets destroyed. code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour {
public float hp;
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Bullet")
{
Destroy(col.gameObject);
hp -= 1f;
}
if (hp < 0.1f)
{
Destroy(gameObject);
}
}
}
and also if there is a better way to tell if the enemy is dead it would be useful. Thanks in advance
well i think thats not even posible, are you sure arent you spawning multiple bullets? even if the bullet wasnt destroyed it shouldnt trigger oncollisionenter multiple times, the bullet gets destroyed before next frame, so i would say the error is somewhere else. just for testing, change destroy to destroyinmediate and check if it continues happening.
i mean the enemy is 2 objects (1 is a child of the other) does it have something to do with that?
both with that same script? if yes thats the problem, only the parent needs the script
Answer by Lawrence_b · Apr 25, 2019 at 07:46 PM
Maybe the collision script should be on the bullet, so when the bullet collides with the enemy it destroys itself. i think the issue is maybe because the enemy health script is accessing the bullet gameobject which doesn't happen instantaneously. Its what id try next anyways