I found a script online (well a few) and they all had the same problems, I assume it’s got something to do with when bullet collides with enemy, destroy enemy. But I changed that and it didn’t work.
var prefab : Transform;
var spawnPoint : Transform;
var spawnRate = 0.8;
private var lastSpawn=0.0;
function Update(){
// error checking
spawnRate=Mathf.Abs(spawnRate);
if(spawnRate > 0.0){
if(Time.time > lastSpawn + spawnRate){
lastSpawn = Time.time;
Instantiate (prefab, spawnPoint.position, spawnPoint.rotation);
}
}
}
The code looks fine to me, and works like a charm. But once I destroy a instantiated prefab none other will spawn, and that’s slightly annoying.
Any ideas?
EDIT:
Bullet Code:
var bulletSpeed : int;
var power : float = 300;
function Update () {
//amount to view bullet
amtToMove = bulletSpeed * Time.deltaTime;
transform.Translate(Vector3.forward * amtToMove);
rigidbody.AddForce(Vector3(0,0,power));
}
function OnBecameInvisible () {
Destroy(gameObject);
}
function OnTriggerEnter(otherObject: Collider){
if(otherObject.gameObject.tag == "enemy1"){
playerScript.playerScore +=50;
Destroy(gameObject);
}
if(otherObject.gameObject.tag == "enemy2"){
playerScript.playerScore +=100;
Destroy(gameObject);
}
}