- Home /
getting loop errors, need help
Im experimenting with loops and am getting errors such as "Assets/Loops.cs(36,22): error CS0103: The name `i' does not exist in the current context"
any ideas why?
using UnityEngine; using System.Collections;
public class Loops : MonoBehaviour {
public class Target {//start class definition
// properties
public int id;
public string type;
public string name;
public int lvl;
public int health;
// A Method
public void targetInfo () {
print("unit: " + id);
print("ID: " + name);
print("class: " + type);
print("level: " + lvl);
print("health: " + health);
}
} //end of class definition
//-------------------------------
// Use this for initialization
void Start () {
Target[] unit = new Target[51];//create array
for (int i = 1; i <= 50; i++);
{
unit[i] = new Target();//instantiating 50 objects
unit[i].id = i;
unit[i].type = "Merc";
unit[i].name = "Yuri";
unit[i].lvl = 12;
unit[i].health = 200;
}
for (int i = 1; i <= 10; i++);
{
unit[i].targetInfo();
}
}
}
@keithe180 please mark robertbu's answer as a correct answer.
Answer by robertbu · Jan 20, 2014 at 03:00 AM
Your issue is on this line:
for (int i = 1; i <= 50; i++);
See the ';' at the end? That indicates the for() loop is executing an empty statement. That also means that the lines of code beyond are out of scope for 'i'. Note you've made this mistake twice in this script. Remove the ';' both places, and your code will compile fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
wierd not existing in current context error caused by 2 lines of code 2 Answers
Unity says scripts don't exist...but they do... 3 Answers
Compilor Error using Dictonary collection 1 Answer
The name PreviewEditorWindow does not exist in this current context 2 Answers
"The name 'Convert' does not exist in the current context" C# 2 Answers