I want to spawn my NPC base on the spawn point I created, but look like I did something wrong. It works but the FPS drop very low every time it spawns.
public Transform[] m_SpawnPoints;
public GameObject m_Golem;
public int SpawnTime;
void Start () {
QualitySettings.vSyncCount = 0;
Invoke ("spawn",1);
}
// Update is called once per frame
void spawn(){
for (int i = 0; i < m_SpawnPoints.Length; i++) {
GameObject m_SpawnGolem = (GameObject)Instantiate (m_Golem, m_SpawnPoints [i].position, Quaternion.identity);
}
Invoke ("spawn", SpawnTime);
}
There is nothing wrong with the script that you posted, it should work just fine.
Maybe you forgot to set SpawnTime in inspector.
If you’re planning to Instantiate a lot of objects, then you should consider using object pooling.
Here is a video that will help you set it up: Link
Also I would advice you to modify QualitySettings and other static Unity classes in one function.
Scattering it around your scripts will make your project harder to manage.
The golem that you are instantiating - does it have any scripts that call for debug.logs or anything along those lines? Debug log calls tend to slow things down if they happen a lot.
1 Like
thank you ! I already fixed it! I had change the spawn function into the IEnumerator, and look like it’s much better , thank you guys !
Ah-hah! Good to hear! You are very welcome, even though you solved it yourself
Good on ya!