- Home /
Question by
Jasper1900 · May 23, 2014 at 04:17 PM ·
arrayfunctionautomatic
Array Element number
Hi,
I have an array of classes. This class contains:
public string Name
public int ID
I want my Array[0] to have ID of 0, Array[1] to have ID of 1, etc. Can I do this automatically, now I am doing everything manually...
C# Please,
Thank you!
Comment
Answer by tw1st3d · May 23, 2014 at 04:31 PM
Array[0] = new myClass();
Array[1] = new myClass();
Array[0].Name = "Name1";
Array[0].ID = 0;
Array[1].Name = "Name2";
Array[1].ID = 1;
With a for loop, creating 1-10; (0-9)
for(int i = 0; i < 10; i++) {
Array[i] = new myClass();
Array[i].Name = "Name" + i.toString();
Array[1].ID = i;
}
You could also create a class constructor, as such
class myClass
{
public string Name;
public int ID;
myClass(string name, int id)
{
this.Name = name;
this.ID = id;
}
}
Array[0] = new myClass("Name0", 0);
Yeah, I could use the second option, but I want this to always happen, not only while running my game.
$$anonymous$$aybe something with inspector scripting will work.