unity script to destroy game objects code example
Example 1: unity destroy gameobject
// Destroy this gameobject
Destroy(gameObject);
// Kills the game object in 5 seconds
Destroy(gameObject, 5);
// Removes this script instance from the game object
Destroy(this);
// Removes the rigidbody from the game object
Destroy(GetComponent<Rigidbody>());
Example 2: how to destroy a gameobject after some hits in unity 3d
public float RayLength; public float Angle; // Ignore this private bool MyBool; public float SpeedRacer; public float DragOn; // Ignore this public GameObject Bullet; //Below this will create your raycast hit meaning if this hits something something else will happen. RaycastHit hit; //Ray creation Debug.DrawRay(transform.position, Vector3.down); Ray MyRay = new Ray(position this ray is initialized at, Vector3.whatever direction you want it pointing to); if (!MyBool) { //Creating the ray statement. //Making the ray attached to the object. if (Physics.Raycast(MyRay, out hit, RayLength) { if (your raycast variable.the collider of the object you wanna hit.its tag == "Your object you wanna destroy's tag") { //Put what ever you want to happen here for your case you want to destroy Destroy(Your game object you want hit) } } } } }