Answer the question
In order to leave comments, you need to log in
How to make bullet destruction on collision in Unity?
Good evening! Tell me where in the bullet code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 100f;
public Rigidbody2D rb;
public GameObject bullet;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.right*speed;
}
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.name != "Player")
{
Destroy(gameObject);
}
}
Answer the question
In order to leave comments, you need to log in
In order to avoid such glitches, it is necessary not to destroy the object, but to mark it as destroyed.
Well, then, after working out all the collisions, either destroy it or put it in the pool for reuse.
1. Try to check the existence of an object before accessing its property:
if(gameObject && collider.gameObject.name != "Player")
{
Destroy(gameObject);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question