- Home /
Adding class info to a list on start
I'm trying to manually add some class info to a list. See the commenting.
public class Achievement
{
public int achievementID;
public string achievementName;
public string achievementDescription;
}
//============================================
void Awake()
{
List<Achievement> create = new List<Achievement>();
//Can't figure out what to type here...
create.Add();
//Want this kind of functionality:
create[0].achievementID = 0;
create[0].achievementName = "Achievement One";
create[0].achievementDescription = "Description";
create[1].achievementID = 1;
create[1].achievementName = "Achievement Two";
create[1].achievementDescription = "Description";
}
Answer by sneftel · Jun 20, 2011 at 06:38 PM
There's a couple of options. First of all:
Achievement a = new Achievement();
a.achievementID = 0;
a.achievementName = "Achievement One";
a.achievementDescription = "Description";
create.Add(a);
Another option is to add a constructor for Achievement taking those three params, such that you can:
create.Add(new Achievement(0, "Achievement One", "Description"));
Oh, and I guess if you wanted you could just do create.Add(new Achievement())
and then set the members from within the container. But that's ugly.
BTW, it's a little silly that all three members of Achievement start with "achievement". How about just id
, name
, and description
? C# will remember what you mean.
That works perfectly, thanks. I started them with "achievement" because I thought words like name/id on their own might conflict with something inside Unity, but maybe not.