The bullet never destroyed!!!

Hello guys, i have a problem with my bullet. I tried to do that bullet destroy themselves but failed. bullet did not destroy. Do not know why … btw I’m learning please help me. I want to add it to my atack script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Atack : MonoBehaviour
{

    public GameObject bullet;
    public float view = 100.0f;
    public float wait = 1.0f; // how much time wait for next shot
    public float counttheshot = 1.0f; // count to the next shot
    public float damage = 50.0f;
    public float timeoflife = 2.0f; // time of life bullet


    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (counttheshot < wait)
            counttheshot += Time.deltaTime;

        if (Input.GetMouseButton(0) && counttheshot >= wait)
        {
            counttheshot = 0;

            Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray, out hitInfo, view))
            {
                Vector3 hitPoint = hitInfo.point;
                GameObject go = hitInfo.collider.gameObject;

                hit(go);

                if (bullet != null)
                    Instantiate(bullet, hitPoint, Quaternion.identity);

            }
        }
        // there is it
        timeoflife -= Time.deltaTime;

        GameObject cloneBullet = Instantiate(bullet);
        if (timeoflife <= 0)
            Destroy(cloneBullet, timeoflife);

    }
}

I boiled your code down to work with the essential thing you are asking about.

using UnityEngine;

public class Atack : MonoBehaviour
{
    public GameObject bulletPrefab;

    public float shotCooldown = 1.0f; // how much time wait for next shot
    private float shotTimer; // count to the next shot
    public float timeoflife = 2.0f; // time of life bullet

    void Update()
    {
        if (shotTimer <= 0)
        {
            if (Input.GetMouseButton(0))
            {
                shotTimer = shotCooldown;

                //Instatiating with reference
                GameObject bullet = Instantiate(bulletPrefab, Vector3.zero, Quaternion.identity);
                Destroy(bullet, timeoflife);
            }
        }
        else
        {
            shotTimer -= Time.deltaTime;
        }
    }
}

I’m doing the cooldown for shooting a bit different, setting it and counting down until it skips over zero. But this is just my preference how to do this.
The essential part is instantiating with a reference and passing the reference to Destroy(). Also the second attribute you pass to Destroy() already does everything for you, no need for calculating the delay yourself (“Fire and forget” haha cough).

if (bullet != null)
Instantiate(bullet, hitPoint, Quaternion.identity);

You never pass a reference to this object. This is what you want to destroy.

GameObject cloneBullet = Instantiate(bullet);
         if (timeoflife <= 0)
             Destroy(cloneBullet, timeoflife);

This only creates an object and then destroys it (if timeoflife <= 0).

You need to have your bullet-reference outside of the Update()-loop.

// Update is called once per frame
GameObject myBulletCopy = null;
     void Update()
     {
         if (counttheshot < wait)
             counttheshot += Time.deltaTime;
 
         if (Input.GetMouseButton(0) && counttheshot >= wait)
         {
             counttheshot = 0;
 
             Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
             RaycastHit hitInfo;
 
             if (Physics.Raycast(ray, out hitInfo, view))
             {
                 Vector3 hitPoint = hitInfo.point;
                 GameObject go = hitInfo.collider.gameObject;
 
                 hit(go);
 
                 if (bullet != null && myBulletCopy == null)
                     myBulletCopy = Instantiate(bullet, hitPoint, Quaternion.identity);
 
             }
         }
         // there is it
         timeoflife -= Time.deltaTime;
         if (timeoflife <= 0)
             Destroy(myBulletCopy);
             myBulletCopy = null;
             timeoflife = 2.0f;
 
     }
 }

If you want more than one bullet you will have to start working with lists or arrays or even better, a custom Bullet-class which handles destruction. Something like:

public class Bullet : Monobehaviour{

public float lifeTime = 2.0f;

void Update()
{
lifeTime -= Time.deltaTime;
if(lifeTime <= 0){
Destroy(this.gameobject);
}
}

}

Add it to your bulletPrefab.