- Home /
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
Answer by Khada · Mar 24, 2013 at 10:27 AM
 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 ins$$anonymous$$d: Cannot implicitly convert type int' to bool' 
Hmmm det bliver desværre heller ikke meget bedre af det :-)
Here's all the code
 using UnityEngine;
 using System.Collections;
 
 public class CarScript : $$anonymous$$onoBehaviour {
     
 
     // 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'
Answer by chrisspinu · Mar 21, 2020 at 04:38 PM
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.
Your answer
 
 
             Follow this Question
Related Questions
C# for loop with 2 ints 2 Answers
How to make wheels turn more? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using for as while. 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                