I’m still learning Unity but I’m not sure how to delete a bullet clone when it touches a game object. I had this problem where I would put in Destroy(gameObject); when it collides with something, but when I test it, it has an error where it deletes the bullet and not the clones. How can I make it so it only deletes the clones when the clones collides with a wall or enemy?
gameObject refers to the object that script is attached to. Make sure you use that function in right script.
Or you can use;
void OnTriggerEnter(Collider collider){
Destroy(collider.gameObject) //the object that collided with your character.
}
When you mean clones are u meaning Prefabs?
if so please give me both scripts you have written
This is the code I used to make the bullet shoot. (got it from a Brackeys video)
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
}
}