How do I make my rockets actually do damage after colliding with something?

As it says above, I fire the rockets and they come out just fine, however whenever they collide with something nothing happens, no damage, not even even an explosion effect. Here’s the script if it helps, oh and btw this script is separate from the script I have on my rocket launcher, this is the script for the rockets only, since it’s the rockets that I want to do the damage of course.

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

public class RocketExplosion : MonoBehaviour
{
    public float speed = 8f;
    public float lifeDuration = 2f;
    public int gunDamage;
    public GameObject impactEffect;

    private float lifeTimer;

    // Start is called before the first frame update
    void Start()

    {
        lifeTimer = lifeDuration;
    }

    // Update is called once per frame
    void Update()
    {
        // Make the bullet move.
        transform.position += transform.forward * speed * Time.deltaTime;

        // Check if the bullet should be destroyed.


        void OnCollisionEnter(Collision col)
        {
            if(col.gameObject.name == "cube(4)")
            {
                Health health = col.transform.GetComponent<Health>();
                if(health != null)
                {
                    health.Damage(gunDamage);
                }
            }
        }
        lifeTimer -= Time.deltaTime;
        if (lifeTimer <= 0f)
        {
            Destroy(gameObject);
        }
    }
}

You have a function inside the update loop. Move OnCollisionEnter(){} outside the Update(){} function. It is like Update() in that it gets called automatically by the engine.

2 Likes