Hey there,
I have a Problem with the “yield return new waitForSeconds…” on Line 17. Everything works fine til I call the yield… I added a Debug.Log(…); in the Line bevor the call and one after. After coming to the yield it doesn’t do anything else… I even added an enormeous while-loop before it and it went through the whole loop, so no stopping from outside I assume…
Thanks for your help,
Btother
using System.Collections;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] objsToSpawn;
public Transform[] spawnPlaces;
public float minWait = .3f;
public float maxWait = 1f;
public void Start()
{
StartCoroutine(SpawnFruits());
}
private IEnumerator SpawnFruits()
{
while (true)
{
yield return new WaitForSeconds(Random.Range(minWait, maxWait));
Transform t = spawnPlaces[Random.Range(0, spawnPlaces.Length)];
float zRotation = Random.Range(-2, 2);
if (t == spawnPlaces[0])
t.transform.Rotate(new Vector3(0, 0, zRotation));
GameObject go;
float p = Random.Range(0, 100);
if (p < 20)
go = objsToSpawn[0];
else
go = objsToSpawn[Random.Range(1, objsToSpawn.Length)];
GameObject fruit = Instantiate(go, t.position, go.transform.rotation);
fruit.GetComponent<Rigidbody2D>().AddForce(t.transform.up * Random.Range(10f, 15f), ForceMode2D.Impulse);
if (t == spawnPlaces[0])
t.transform.Rotate(new Vector3(0, 0, -zRotation));
Destroy(fruit, 5f);
}
}
}