Spawning 1 object after enemy destroyed

Hello!
In my unity game I have several UFO enemies that spawn minions and are you’know… really mean bad guys. When the player shoots down a UFO, I made it so that a special microchip object spawns from the UFO and has a script on it so that it will follow the player and the player will collect these microchips (thus adding points + winning the game once a certain amount of microchips are earned).

The problem is the UFO spawns like 3 at a time and I feel like maybe it’s because the way my script is set up, it spawns as many microchips as it took bullets to hit it.

I put the spawn chip in the bullet script, so therefore when it gets hit, the hit is registered and triggers the spawning to happen. Can anyone help me figure out how to only make the microchip spawn once? Thanks!

CODE

var Microchip : GameObject;
var spawn_position;

function Start(){

}

//Ignore this part. This is just if the bullet collides with enemies breakablewalls

function OnCollisionEnter(collision : Collision) {
	if (collision.gameObject.tag == "BreakWall" || collision.gameObject.tag == "Enemy"){ 
		Destroy(collision.gameObject, 3.0); 
		Destroy(this.gameObject);// destroy it
		}

	if (collision.gameObject.tag == "UFO"){ **//This is where the UFO collision happens**
		spawnChip ();
		Destroy(collision.gameObject); **//destroy the UFO**
		Destroy(this.gameObject);// **destroy the Bullet**
		}
	}

function spawnChip () {

spawn_position = Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
var temp_spawnChip = Instantiate(Microchip, spawn_position, Quaternion.identity);
}

I would create a function OnDestroy() that is inherited from monobehavior and put spawnChip() in that. It will only get called once and will be called right before the object is destroyed.

Link: Unity - Scripting API: MonoBehaviour.OnDestroy()

You could try just adding in a simple variable to help with only spawning 1 chip. For example:

var Microchip : GameObject; var spawn_position;

var CanSpawnChip = true;

function OnCollisionEnter(collision : Collision) {
    if (collision.gameObject.tag == "BreakWall" || collision.gameObject.tag == "Enemy"){ 
       Destroy(collision.gameObject, 3.0); 
       Destroy(this.gameObject);
       }
 
 
    if (collision.gameObject.tag == "UFO"){
       if(CanSpawnChip == true){
       spawnChip ();
       CanSpawnChip = false;
       }
       Destroy(collision.gameObject); 
       Destroy(this.gameObject);
       }
    }

thanks! Both answers were great, but the OnDestroy function really worked smoothly! I had no idea that existed, thanks for pointing that out!