Make Bullet Destroy on Impact

Hey everybody I’m just trying to have my bullet destroy on impact. My bullet is a prefab. Here is my unity screen

Here is my code:

public var prefabBullet:Transform; //object to create
public var bulletSpawnPoint:Transform; //place to create object
public var shootForce:float; //speed of object

function Start () {

}

function Update ()
{
if(Input.GetButton(“Fire1”)) //if left click is pressed
{
//create the bullet
var bulletInstance = Instantiate(prefabBullet,bulletSpawnPoint.position, bulletSpawnPoint.rotation);

//fire the bullet
bulletInstance.rigidbody.AddForce(bulletSpawnPoint.transform.forward*shootForce);
}

}

function OnCollisionEnter(c:Collision)
{
if(c.gameObject.name == “Bullet”)
{

Destroy(c.gameObject);

}

}

Every time I shoot the plane it just bounces off! What can I do???

What happens is that since the script is attached to the player, the bullet will only be destroyed when it hits the player, not the plane. If you want the bullet to be destroyed when it hits the plane, try creating a new script and moving the OnCollisionEnter there before attaching that new script to the plane.

Although, I much prefer it if the bullet is the one checking the collision and it destroying itself if it collides with something, just my opinion though.

Ok so I made the my “Bullet” (Prefab) a gameobject

and I attached my code to the gameobject

Here is my code:

#pragma strict
public var prefabBullet:Transform; //object to create
public var bulletSpawnPoint:Transform; //place to create object
public var shootForce:float; //speed of object

function Start ()
{

}

function Update ()
{
if(Input.GetButton(“Fire1”)) //if left click is pressed

//create the bullet
var bulletInstance = Instantiate(prefabBullet,bulletSpawnPoint.position , bulletSpawnPoint.rotation);

//fire the bullet
bulletInstance.rigidbody.AddForce(bulletSpawnPoint .transform.forward*shootForce);

}

function OnCollisionEnter(c:Collision)
{
if(c.gameObject.name == “Bullet_Prefab”)
{

Destroy(c.gameObject);

}

}

The Bullet still won’t disappear and I get errors, here they are:

NullReferenceException: Object reference not set to an instance of an object
BulletCollide.Update () (at Assets/BulletCollide.js:19)

Try putting a script on the bullet that will destroy it when it collides. Of course, depending on the velocity of the bullet you may want to use Physics.Raycast instead, and just instantiate a particle prefab when the Raycast starts and hits.