Question by
jdswanson83 · Oct 31, 2015 at 12:59 AM ·
start
sets/Scripts/GameController.cs(15,10): error CS1624: The body of `GameController.Start()' cannot be an iterator block because `void' is not an iterator interface type
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public Transform enemy;
public float timeBeforeSpawning = 1.5f;
public float timeBetweenEnemies = .25f;
public float timeBeforeWaves = 2.0f;
public int enemiesPerWave = 10;
private int currentNumberOfEnemies = 0;
// Use this for initialization
void Start()
{
StartCoroutine(SpawnEnemies());
IEnumerator SpawnEnemies;
{
yield return new WaitForSeconds(timeBeforeSpawning);
while (true)
{
if (currentNumberOfEnemies <= 0)
{
float randDirection;
float randDistance;
for (int i = 0; i < enemiesPerWave; i++)
{
randDistance = Random.Range(10, 25);
randDirection = Random.Range(0, 360);
float posX = this.transform.position.x + (Mathf.
Cos((randDirection) * Mathf.Deg2Rad) * randDistance);
float posY = this.transform.position.y + (Mathf.
Sin((randDirection) * Mathf.Deg2Rad) * randDistance);
Instantiate(enemy, new Vector3(posX, posY, 0), this.transform.rotation);
currentNumberOfEnemies++;
yield return new WaitForSeconds(timeBetweenEnemies);
}
}
}
}
}
// Update is called once per frame
void Update()
{
}
public void KilledEnemy()
{
currentNumberOfEnemies--;
}
}
Comment
Answer by hexagonius · Oct 31, 2015 at 11:19 AM
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public Transform enemy;
public float timeBeforeSpawning = 1.5f;
public float timeBetweenEnemies = .25f;
public float timeBeforeWaves = 2.0f;
public int enemiesPerWave = 10;
private int currentNumberOfEnemies = 0;
// Use this for initialization
void Start()
{
StartCoroutine(SpawnEnemies());
}
IEnumerator SpawnEnemies()
{
yield return new WaitForSeconds(timeBeforeSpawning);
while (true)
{
if (currentNumberOfEnemies <= 0)
{
float randDirection;
float randDistance;
for (int i = 0; i < enemiesPerWave; i++)
{
randDistance = Random.Range(10, 25);
randDirection = Random.Range(0, 360);
float posX = this.transform.position.x + (Mathf.
Cos((randDirection) * Mathf.Deg2Rad) * randDistance);
float posY = this.transform.position.y + (Mathf.
Sin((randDirection) * Mathf.Deg2Rad) * randDistance);
Instantiate(enemy, new Vector3(posX, posY, 0), this.transform.rotation);
currentNumberOfEnemies++;
yield return new WaitForSeconds(timeBetweenEnemies);
}
}
}
}
public void KilledEnemy()
{
currentNumberOfEnemies--;
}
}
Your answer
Follow this Question
Related Questions
How do i get started with unity? (C#) 2 Answers
Start function not called after reloading scene 0 Answers
Initiating random camera on start? 2 Answers
,Wait x seconds for game to start. 1 Answer