Good evening!
I’ve encountered a wall, I might need help. Here it is:
I want my to be in total control of what my IEnumerator is doing right there and for that I need to be able to stop the Coroutine but for some reason it just won’t stop when I want it to.
The script bellow is just one of many test I’ve done and none are successful. Maybe IEnumerator is not the way to go? What are your thought??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundControllerFast : MonoBehaviour
{
public GameObject[] landscapes;
public Vector3 spawnValues;
public int landscapeCount;
public float startWait;
public float spawnRate;
public float waveWait;
void Update()
{
bool down = Input.GetKeyDown("r");
bool up = Input.GetKeyUp("r");
if (down)
{
StartCoroutine(LandscapeFast());
}
if (up)
{
StopCoroutine(LandscapeFast());
}
}
public IEnumerator LandscapeFast()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < landscapeCount; i++)
{
GameObject landscape = landscapes[Random.Range(0, landscapes.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(landscape, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnRate);
}
yield return new WaitForSeconds(waveWait);
}
}