- Home /
How to make a for loop for a class in C#
print("Hello World")
I was using the FPS1 tutorial set(http://forum.unity3d.com/threads/139276-ETeeskiTutorials-FPS1-42-Inverse-Kinematic-Arm-Tutorial-scripts-and-project-download) and translating it to c#. I wanted make a gun manager. I made a class containing all necessary public variables and I made an array of classes.
Question: How do I go through this array? Thanks in advance
Answer by sharpshot124 · Jan 02, 2014 at 08:08 PM
for loops are very easy, practically the same as they are in js.
for(int i = 0; i < classArray.Length; i++)
{
classArray[i] = new ClassType();
// or
ClassType item = classArray[i];
// item.functions(arguments);
// item.variables = values;
}
or if you don't need to set the value if the array you can do a foreach
foreach(ClassType item in classArray)
{
//item.functions(arguments);
//item.variables = values;
}
alternatively for the foreach you can do
foreach(var item in classArray)
and it will create the type of the item variable for you based off the type of the classArray variable
oh wow. I fail. tried making the ClassType item part outside of the foreach loop. Thanks this has been a huge help.
Your answer
Follow this Question
Related Questions
Card/Tile Game, loops dont update element0 of playerHand properly 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need my function to work with different lists of different values (classes) 1 Answer
Make custom editor for generic class to apply to all current and future child classes 1 Answer