gameObject.SetActive(false) doesn't work with bullet movement

Hello everyone, I’m a beginner and the answer to this is probably easy and I’m being dumb.
Anyway, I’m moving my bullet with AddForce on the start method to move it when it’s spawned.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{

    public Rigidbody rb;
    public GameObject impactEffect;
    public float speed = 1000F;
    public int damage = 5;
    public static int bulletDamage;

    private ObjectPool objectPool;
    
    void Start()
    {

        bulletDamage = damage;
        objectPool = FindObjectOfType<ObjectPool>();
        rb = GetComponent<Rigidbody>();
        rb.AddForce(transform.forward * speed * 100 * Time.deltaTime);

    }

    private void Update()
    {

        

    }

}

But if I add this to it at the bottom:

 public void OnTriggerEnter(Collider collider)
    {

        GameObject newEffect = objectPool.GetObject(impactEffect);
        newEffect.transform.position = transform.position;
        newEffect.transform.rotation = transform.rotation;

        gameObject.SetActive(false);

    }

It doesn’t work. The thing above the SetActive is just something that spawns an impact effect when it hits a trigger and that works, the problem is that when I add it my bullet doesn’t move the way I want it to.

The game I’m working on is a Tower Defence game and the bullet is coming out of a turret and it shoots straight to the enemy if I don’t add the SetActive, but If I do add it the bullet just hits the ground in one spot.

I’ll give the turret code in case that’s relevant:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Turret : MonoBehaviour
{
        
    [Header("Target Tag")]

    // Public
    
    public string alienTag = "Alien";

    [Header("Rotation")]

    // Public

    public Transform rotationPoint;
    public float rotationSpeed = 10F;

    [Header("Shooting")]

    // Public

    public LayerMask layerMask;
    public GameObject bulletPrefab;
    public string bulletTag;
    public Transform firePoint;
    public float range = 50F;

    [Header("Ammo & Bullet Parameters")]

    // Public
    
    public float fireRate = 1F;
    public int maxAmmo = 15;
    public float reloadTime = 5F;

    // Private

    private int currentAmmo;
    private bool isReloading = false;

    // Private variables

    private ObjectPool objectPool;
    private Transform target;
    private float fireCountDown = 0F;
    
    void Start()
    {

        objectPool = FindObjectOfType<ObjectPool>();
        currentAmmo = maxAmmo;
        InvokeRepeating("UpdateTarget", 0F, 0.5F);
        
    }

    private void OnEnable()
    {

        isReloading = false;
        
    }

    void UpdateTarget()
    {

        GameObject[] aliens = GameObject.FindGameObjectsWithTag(alienTag);

        float shortestDistance = Mathf.Infinity;
        GameObject nearestAlien = null;

        foreach (GameObject alien in aliens)
        {

            float distanceToAlien = Vector3.Distance(transform.position, alien.transform.position);
            if(distanceToAlien < shortestDistance)
            {

                shortestDistance = distanceToAlien;
                nearestAlien = alien;
                
            }
            
        }

        if(nearestAlien != null && shortestDistance <= range)
        {

            target = nearestAlien.transform;
            
        } else
        {

            target = null;
            
        }
        
    }

    void Update()
    {

        Ray ray = new Ray(firePoint.position, firePoint.forward);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, range, layerMask))
        {

            Debug.DrawLine(firePoint.position, hit.point, Color.green);
            Debug.Log("Target found");

            if (fireCountDown <= 0F)
            {

                Shoot();
                fireCountDown = 1F / fireRate;

            }

            fireCountDown -= Time.deltaTime;

        }
        else
        {

            Debug.DrawLine(firePoint.position, hit.point + ray.direction, Color.red);

        }

        if (isReloading)
        {

            return;
            
        }
        
        if(currentAmmo <= 0)
        {

            StartCoroutine(Reload());
            return;
            
        }
        
        if(target == null)
        {

            return;
            
        }

        Vector3 targetDir = target.position - rotationPoint.position;
        Quaternion lookRotation = Quaternion.LookRotation(targetDir);
        rotationPoint.rotation = Quaternion.Lerp(rotationPoint.rotation, lookRotation, Time.deltaTime * rotationSpeed);

    }

    public void Shoot()
    {
        
        currentAmmo--;
        
        Debug.Log("Shoot");
        GameObject newBullet = objectPool.GetObject(bulletPrefab);
        newBullet.transform.position = firePoint.transform.position;
        newBullet.transform.rotation = firePoint.transform.rotation;


    }

    IEnumerator Reload()
    {

        isReloading = true;
        
        Debug.Log("Reloading...");

        yield return new WaitForSeconds(reloadTime);

        currentAmmo = maxAmmo;

        isReloading = false;
        
    }

    public void OnTriggerEnter(Collider collider)
    {
        
        if(collider.CompareTag("Bullet"))
        {
            
            
            
        }
        
    }

    private void OnDrawGizmos()
    {

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range);
        
    }

}

Any help is appreciated.

Hi @Minigeek0105,

Check few things:


(1) You need to be sure that RigidBody Component attached to your Bullet GameObject is not marked as “isKinematic”… so physica could affect it in normal way.


(2) Cut off empty void Update() form Bullet script. As long as selected ForceMode selected in RigidBody.AddForce is not continuous added force we not need Update / fixedUpdate…


(3) on Bullet script add:

public void ShootBullet(Vector3 _endOfMuzzlePosition, Quaternion _innitialRotation)
{
this.transform.positon = _endOfMuzzlePosition;
this.transform.rotation = _innitialRotation;
//if using pool system, here reaactivate all of atached components, awake RigidBody, turn on MeshRenders, Turn on ParticleSystem, and start emitting etc...
rb.AddForce(transform.forward * speed * 100, ForceMode.Impulse);  //as you see no need to Time.deltaTime which is used only in Update/FixedUpdate.
}

then you could call it directly after grabbing Bullet from Pool… also consider no need for having pool of GameObjects, you could have use Pool of Bullets, so you will be able to acces directly void
ShootBullet().


(4) this ‘public void OnTriggerEnter(Collider collider)’ void is about what will hapend when your Bullet hit something, don’t hav anything to do with bullet movement.