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);
}