IEnumerator SpawnAsteroid() { //Coroutine that spaw asteroids with period float spawnPeriod; Vector3 asteroidCoordinates; while (1 == 1) { spawnPeriod = NextAsteroidTime(); asteroidCoordinates = road.GetCoordinatesForAsteroid(); asteroidCoordinates.x = Random.Range(-spaceShip.GetRoadBorderCoordinates(), spaceShip.GetRoadBorderCoordinates()); asteroidCoordinates.y = 2.3f; asteroidCoordinates.z = Random.Range(asteroidCoordinates.z - 5, asteroidCoordinates.z + 5); Instantiate(asteroidPrefab, asteroidCoordinates, Quaternion.identity); spaceShip.ChangeSpeed(1); yield return(new WaitForSeconds(Random.Range(spawnPeriod, spawnPeriod + 0.1f))); } }