Basically, I wrote this level generating script for an endless runner game in Unity. The problem is that the boards generate way too fast for any mobile device to handle. To prevent this, I added this line of code:
yield return new WaitForSeconds(2);
But for some reason, my script keeps generating boards as fast as it can, not waiting for 2 seconds.
I tried toggle commenting the line, but whatever I tried didn't work. Can anyone take a look?
Here's the full script:
using System.Collections.Generic;
using UnityEngine;
public class GenerateLevel : MonoBehaviour
{
public GameObject[] section;
public int zPos = 19;
public bool creatingSection = false;
public int secNum;
void Update()
{
if (creatingSection == false)
creatingSection = true;
StartCoroutine(GenerateSection());
}
IEnumerator GenerateSection()
{
secNum = Random.Range(0, 3);
Instantiate(section[secNum], new Vector3(0, 0, zPos), Quaternion.identity);
zPos += 38;
yield return new WaitForSeconds(2);
creatingSection = false;
}
}