- Home /
How to translate code with yield waitforseconds to c#?
I'm trying to translate a spawner code into c# but I keep getting errors.
function spawnMinion () {
for (var x = 1; x < 8; x++) {
yield WaitForSeconds(0.2);
Instantiate(minionPrefab,node1.transform.position,node1.transform.rotation);
if(x == 7 ) {
yield WaitForSeconds(5);
x -= 8;
}
}
}
I tried this but it doesn't want to work
void Awake ()
{
StartCoroutine(Spawn());
wave = GameObject.Find("Wave").GetComponent<Wave>();
}
public IEnumerator Spawn()
{
int enemyIndex = Random.Range (0, enemies.Length);
for (var x = 1; x < 8; x++) {
yield return new WaitForSeconds (0.2);
Instantiate (enemies [enemyIndex], transform.position, transform.rotation);
if (x == 7) {
yield return new WaitForSeconds (5);
x -= 8;
Wave.wavenumber += 1;
}
}
}
Can anyone help me translate the first code into c#?
Answer by oasisunknown · Jun 07, 2014 at 04:05 AM
the first mistake I see in the second code is.
for (var x = 1; x < 8; x++)
C# does not use vars change it to int.
thats all I see.
I found the error, apparently I just had to rewrite the waitforseconds for some reason. I think vars work for c#, I used it for my gui.
At least you found your issue.
and just as a test I tried declaring
var x = 1; // in C#
and it gave this error to the console
Assets/Scripts/SetActive.cs(6,9): error CS0825: The contextual keyword `var' may only appear within a local variable declaration
and the for loop would be a local variable.
I just tested it and because the for loop is a local variable you can use var there in C#
the test code.
for (var x = 1; x < 8; x++)
{
Debug.Log ("This is a number" + x);
}
Your answer
Follow this Question
Related Questions
Delay If statement in Update c# 2 Answers
Create a delay between function executions 1 Answer
Multiple Cars not working 1 Answer
Delay in a IEnumerator 2 Answers
Distribute terrain in zones 3 Answers