- Home /
Generic List - Class Add Dynamically
Hello,
I'm trying to make a List of my own Class. I don't know how to populate it though, I keep running into Errors. So below is fine, this all displays well in my Inspector, and I can add Elements and it shows fine.
class QUIZ_SUB_QUESTION{
var question : String;
var answers : List.<String>;
var cindex : int;
}
var questions : List.<QUIZ_SUB_QUESTION>;
Now, I want to Add to the List dynamically:
var qList : List.<QUIZ_SUB_QUESTION> = new List.<QUIZ_SUB_QUESTION>();
qList..question = question;
qList.answers = null;
qList.cindex = cindex;
questions.Add(qList);
But I get the errors:
'question' is not a member of 'System.Collections.Generic.List.
'answers' is not a member of 'System.Collections.Generic.List.
'cindex' is not a member of 'System.Collections.Generic.List.
When they clearly are ... what am I doing wrong here? Thanks.
Answer by NoseKills · Nov 26, 2015 at 04:45 PM
The way you have now done it is that both qList
and questions
are of type List<QUIZ_SUB_QUESTION>
so you are trying to make a list of lists.
You should be doing something like
var question : QUIZ_SUB_QUESTION = new QUIZ_SUB_QUESTION();
question.question = "How old are you?";
question.answers = null;
questions.Add(question);
'question' is not a member of 'System.Collections.Generic.List.
The compiler is confused because you are trying to set the .question variable of the list. A list doesn't have such a variable , but each QUIZ_SUB_QUESTION in the list does because you declared it in your class.
Your answer
![](https://koobas.hobune.stream/wayback/20220612050450im_/https://answers.unity.com/themes/thub/images/avi.jpg)