- Home /
Bullet Destroying script
Hello. trying to get my bullet to get destroyed on collision but i can't make it work. please Help.
var maxDistance = 50;
var LookAtTarget:Transform;
var bullitPrefab:GameObject;
var damp = 6.0;
var savedTime = 0;
function Update () {
if(LookAtTarget && distance < maxDistance)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * damp);
var seconds : int = Time.time;
var evenodd = (seconds / 2);
if (evenodd)
{
Shoot(seconds);
}
}
}
function Shoot(seconds) {
if(seconds!=savedTime)
{
var bullit = Instantiate(bullitPrefab,transform.Find("spawnPoint").transform.position,
Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * speed);
savedTime = (seconds);
if (collision : Collision){
Destroy(bullitPrefab.)
}
} function OnCollisionEnter(collision : Collision) {
// Remove the Bullet from the world
Destroy(bullitPrefab.gameObject);
}
Answer by SkaredCreations · Dec 18, 2014 at 10:34 PM
With Destroy(bullitPrefab.gameObject)
you're trying to destroy the prefab itself and not the instance you created with Instantiate. Anyway you should really move the function OnCollisionEnter
into a script that you'll attach to your bullet prefab (but replacing bullitPrefab.gameObject
with gameObject
), so it's running on the bullet and will destroy itself (that is the correct logic).
Your answer
![](https://koobas.hobune.stream/wayback/20220613174357im_/https://answers.unity.com/themes/thub/images/avi.jpg)