Checking and destroying a rigidbody?

I’m trying to make a script that detects when a bullet hits an enemy, it will destroy the bullet, lower health, and if health is low enough, destroy the enemy. The code has been simplified to include on the health function.

public Collision bulletcolide;  //Would you use this to designate the bullet collier?
    public int health = 3;
    
    void Update () {
    		HealthFunc(bulletcolide);
    		}
    
    void HealthFunc(Collision collision){ 
    		if(collision.rigidbody)
    		{
    			health = health -1;
    			Destroy (collision.gameObject);
    		}
    		if(health < 1)
    		{
    			Destroy (gameObject);
    		}
    
    	}

The way i would do it is if the script is attached to the bullet then you could put this code in

void OnCollisionEnter(Collision collision)
	{
                if (collision.gameObject.tag == "Enemy") 
		{
			Destroy(gameObject);
			(enemy script name) = collision.gameObject.GetComponent<(enemy script name)>();
			(enemy script name).enemyHealth -= 1;
		}
	}

that will destroy the bullet and take health from the enemy.
you also need to attach a script to the enemy and create the health variable and in the bullet script you need to get the enemy script component.

To destroy the enemy when it dies you need to put
if(enemyHealth <= 0)
{
Destroy(gameObject);
}

in the update function of the enemy script

Hey PenquinEmporium!

The easiest method I see for this is to have a “Bullet” Component on the bullets that you shoot.
Like so:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(SphereCollider))]
public class Bullet : MonoBehaviour
{
	void Update()
	{
		//Update your projectile/trajectory/etc
	}

	public void Impact()
	{
		//Do whatever you need to here
		Destroy (gameObject);
	}

	void OnCollisionEnter(Collision other)
	{
		if(other.gameObject.GetComponent<Player>())
		{
			Impact();
			other.gameObject.GetComponent<Player>().HealthFunc();
		}
	}
}

Then from that in your “Player” class or whatever you have called it, you can use your health function like so:

void HealthFunc()
{ 
	health--;

	if(health <= 0)
		Destroy (gameObject);		
}

Also there are a bunch of other messages that Components offer!
Check here for all messages available on colliders

I hope this helps!

-Peace