For loop C#?

Hello, I can’t get my for loop working, this is the code:

public int test = 1;	
	
IEnumerator RotateWheelLeft() {
	for (test i = 0; i = 50; i++) {
			// stuff
		}
			
	}

The error is: CarScript.test' is a field’ but a `type’ was expected

Thanks, Andreas

2 Answers

2

test i = 0; //only makes sense if ‘test’ is a type that can be set to 0
int i = 0; //an int is a type that can be set to 0 and used as a loop counter

All variable declarations use the following structure:

//type   name   =   value
  int    num    =   0;

Okay, now I changed the code to this IEnumerator RotateWheelLeft() { for (int i = 0; i = 50; i++) { //Stuff } } But I now get this error instead: Cannot implicitly convert type int' to bool'

–

It should be: for(int i = 0; i < 50; i++) (I think)

–

Hmmm det bliver desværre heller ikke meget bedre af det :-)

–

Here's all the code using UnityEngine; using System.Collections; public class CarScript : MonoBehaviour { // Use this for initialization void Start () { StartCoroutine("RotateWheelLeft"); } public int g; IEnumerator RotateWheelLeft() { for (int i = 0; i < 50; i++) { g += 1; } } } Error: `CarScript.RotateWheelLeft()': not all code paths return a value

–

Uh, en dansker! Får du fejlen "not all code paths return a value"? I så fald: void RotateWheelLeft() { for (int i = 0; i < 50; i++) { // stuff } } Skal du skifte 'IEnumerator' ud med 'void'

–

for (int i = 0; i == 50; i++) {
// stuff
}
// You need to add two equals

This makes no sense, First you bumped a question that was asked over 7 years ago and has already been answered. Second using == in the condition doesn't make any sense since your loop wouldn't run at all. You set i to 0 and you only run your loop while i is 50 which is not the case to begin with so the loop isn't executed at all.

–