Experienced unity developers, how do you instantiate new things? I have this enemy that shoots a very high stream of bullets. If I get a few of them on screen at once, the framerate drops like crazy. Is it because there are too many objects on-screen? Is it because my way of creating new bullets is not very good? If you have any idea, please tell me :D

public void shoot()
{
GameObject bullet = new GameObject();
if(bulletPattern == 1026) bullet.transform.rotation = parent.rotation;
bullet.tag = "enemyBullet";
bullet.name = parent.name + " bullet";
bullet.AddComponent<bullet_script>();
bullet.GetComponent<bullet_script>().init();
bullet.GetComponent<SpriteRenderer>().sprite = bulletSprite;
bullet.GetComponent<bullet_script>().timeToLive = ttl;
bullet.GetComponent<bullet_script>().fromPlayer = false;
bullet.GetComponent<bullet_script>().pattern = bulletPattern;
bullet.GetComponent<bullet_script>().damage = bulletDamage;
if(giant)
{
bullet.GetComponent<bullet_script>().damage *= 2;
}
int dX = -1;
if(parent.localScale.x < 0f)
{
dX = 1;
}
bullet.GetComponent<bullet_script>().direction = dX;
bullet.transform.position = new Vector2(parent.position.x,parent.position.y);
if(bulletPattern == 1005) bullet.transform.position = new Vector2(parent.position.x,parent.position.y);
if(giant && giantBullets)
{
bullet.transform.localScale = new Vector3(6.72f,6.72f);
}
bullet.GetComponent<bullet_script>().isShot ();
}

Time spent on the project so far: 547 hours

Currently working on: Continuing the story

Posted
AuthorJérémie Tessier