bullet destroy after collision

after adding in the line ‘Destroy(gameObject);’ to the bullet script the enemy no longer shoots. Why is this? I added that line of code so the bullet cannot go through walls.

Enemy Bullet Script:

var Dammage = 50;

function OnCollisionEnter (info : Collision)
{
    info.transform.SendMessage("ApplyDammage1", Dammage,
    SendMessageOptions.DontRequireReceiver);
    Destroy(gameObject, 1);
}

function Update ()
{
    var translation : float = Time.deltaTime * 60;
    transform.Translate(0,0,translation);
}

AI Script:
var Distance;
var Target : Transform;
var ShootRange = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
var projectile : Rigidbody;
var fireRate : float = 1;
var FireRange = 0-100;
private var nextFire : float = 0.0;

function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);

if (Distance < ShootRange)
{
GetComponent.().material.color = Color.yellow;
lookAt();
}

if (Distance > ShootRange)
{
lookAt();

}

if (Distance < attackRange)
{
GetComponent.().material.color = Color.red;
attack ();
}

if (Distance > attackRange && Time.time > nextFire )
{
nextFire = Time.time + fireRate;
Instantiate(projectile, transform.position, transform.rotation);

}
}

function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);

}

function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

How, and where, do you instantiate the bullet? It sounds like you instantiate it at a position where it collides with something immediately. Either check for that in the script (if info.gameObject.tag etc) or move the position somewhere where it can not collide at spawn.

This is my new enemy bullet script but the bullet still goes through walls. I am really confused now :frowning: I changed the walls tag to ‘Level’

var Dammage = 50;

function OnCollisionEnter (info : Collision)
{
    info.transform.SendMessage("ApplyDammage1", Dammage,
    SendMessageOptions.DontRequireReceiver);
    Destroy(gameObject, 1);
    if(info.gameObject.tag == "Level")
    {
        Destroy(gameObject);
    }
}

function Update ()
{
    var translation : float = Time.deltaTime * 60;
    transform.Translate(0,0,translation);
}

You call Destroy twice now, and the first call delays the destroy by one second, I also assume the second one doesn’t do anything as Destroy should only be called ones. So that explains why code-wise, the bullet goes “through” the wall. The second reason is that the physics engine does not do accurate calculations on fast objects, so objects can clip through. It might be better to use a raycast as the bullet, unless you have a reason for using a physical object.

That said, also make sure the bullet has a rigidbody attached, otherwise the OnCollisionEnter method will not be called at all.

thankyou so much!! I deleted the first destroy object line and slowed down the bullets movement speed :slight_smile: