I’m creating a script that shoots bullets and destroys them off screen, it works for the most part and creates (clones), as intended, and destroys them, unity the generates (clone)(clone)(clone) etc and doesn’t destroy them off screen, this severely slows the game down after a couple of shots. How do I stop generating these and only generating (clone) as a pose to (clone)(clone)(clone)'s? Thanks for help in advance.
public class Bullet : MonoBehaviour {
public float offset = 128f;
public GameObject projectilePrefab;
private List<GameObject> Projectiles = new List<GameObject>();
private float projectileVelocity;
void Start (){
projectileVelocity = 3;
}
void Update(){
if (Input.GetButtonDown ("Fire1")) {
GameObject bullet = (GameObject)Instantiate (projectilePrefab, transform.position, Quaternion.identity);
Projectiles.Add (bullet);
}
for (int i = 0; i < Projectiles.Count; i++) {
GameObject goBullet = Projectiles *;*
if (goBullet != null) {
goBullet.transform.Translate (new Vector3 (100, 0) * Time.deltaTime * projectileVelocity);
}
Vector3 bulletScreenPos = (Camera.main.WorldToScreenPoint(goBullet.transform.position));
if (bulletScreenPos.x >= Screen.width + offset || bulletScreenPos.x <= 0) {
DestroyObject (goBullet);
Projectiles.Remove (goBullet);
}
}
}
}