Hello,
I want to create a random 2D level at the start of the game. I made an empty game object and attached a script to it that spawns a given number of a certain object in a given range. Here is my code:
using UnityEngine;
using System.Collections;
public class StarPlacer : MonoBehaviour {
public GameObject[] Stars;
public int Star_Count;
public int Star_Space_Min;
public int Star_Space_Max;
public GameObject star;
public string StarTag;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Stars = GameObject.FindGameObjectsWithTag(StarTag);
if (Stars.Length <= Star_Count)
{
GameObject BackGround_Star = Instantiate(star) as GameObject;
BackGround_Star.transform.position = new Vector3(Random.Range(Star_Space_Min, Star_Space_Max), Random.Range(Star_Space_Min, Star_Space_Max), 0.1f);
}
}
}
The problem with this code is that it spawns one object per frame. I’m trying to spawn 2400 objects.
It doesn’t help that my FPS dies when the game starts so I end up spawning 2400 objects at 3 objects per second!
How can I spawn these objects more efficiently?
EDIT: When the code speaks about stars, it really means any object that I want it to mass spawn.