destroy in unity code example

Example 1: unity remove gameobject

// To remove a GameObject use the function 'Destroy()'
Destroy(gameObject);

Example 2: 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 3: destroy gameobject unity

Destroy(this.gameObject);

Example 4: raycasting in unity

RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Did Hit");
        }

Example 5: how to destroy an object in unity

Destroy(gameObject);

Example 6: how to destroy a gameobject after some hits in unity 3d

public int hit = 0; public GameObject brickParticle; public AudioClip Brick_breaking; void OnCollisionEnter(Collision other) {     hit += 1; } void checkhit() {     if (hit == 2)     {         AudioSource.PlayClipAtPoint(Brick_breaking, transform.position);         Instantiate(brickParticle, transform.position, Quaternion.identity);         GM.instance.DestroyBrick();         Destroy(gameObject);     } }