Item Drops on enemy spawn and death?

so I have a health potion to drop on the enemy death when it basically collides with my other objects and the giving it a random chance 25% at the moment to drop the item (potion). but currently it is dropping on both spawn and destroy. some help would be amazing :smiley:

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

public class DestroyAI : MonoBehaviour {
public GameObject Healthpot;
public float dropRate = 0.25f; //25% drop chance
public Transform HealthPotSpawn;

void OnCollisionEnter (Collision col)
{
	if (col.transform.tag == "Bullet")
		Destroy (gameObject);
	else if (col.transform.tag == "Player")
		Destroy (gameObject);
	else if (col.transform.tag == "Enemy")
		Destroy (gameObject);
	else if (col.transform.tag == "Gooba")
		Destroy (gameObject);


		if (Random.Range (0f, 1f) <= dropRate)
			Instantiate (Healthpot, HealthPotSpawn.position, HealthPotSpawn.rotation);
	}

}

look where you place your drop function

You need curly braces around the last “else if” response.

void OnCollisionEnter (Collision col)
 {
     if (col.transform.tag == "Bullet")
         Destroy (gameObject);
     else if (col.transform.tag == "Player")
         Destroy (gameObject);
     else if (col.transform.tag == "Enemy")
         Destroy (gameObject);
     else if (col.transform.tag == "Gooba") { // add open curly
         Destroy (gameObject);
 
         if (Random.Range (0f, 1f) <= dropRate)
             Instantiate (Healthpot, HealthPotSpawn.position, HealthPotSpawn.rotation);
    } // add close curly
} // fix your indenting for clarification

I would also recommend using that syntax for all “if” and “else if” statements as it helps code readability. And I would also recommend reading this article for further clarification: if and switch statements - select a code path to execute | Microsoft Learn