- Home /
how to add list after removing some
okay my problem is i have a QandA game and i created a 50/50 lifeline button which removes two of the wrong answers in the choices so my question is how to get it back after answering that question so i get the logic i need to add again but the problem is it keeps on adding everytime i answered correctly okay here's the code hope you help me
private List<string[]> questions = new List<string[]>();
public List<int> answerOrder = new List<int>(new int[] {1,2,3,4});
void DrawInfo() { Rect rect = new Rect(500, 100, 400, 200);
Rect close = new Rect(10, 10, 200, 100);
if(GUI.Button(close, "Question")) {
PopUp = !PopUp;
}
if (PopUp) {
GUI.Box(rect, Info);
GUI.Label(new Rect(520, 110, 400, 30), questions[0][0]);
if (GUI.Button(new Rect(520, 200, 100, 30), questions[0][answerOrder[0]])) {
HandleAnswer(answerOrder[0]);
}
if (GUI.Button(new Rect(520, 250, 100, 30), questions[0][answerOrder[1]])) {
HandleAnswer(answerOrder[1]);
}
if (GUI.Button(new Rect(780, 200, 100, 30), questions[0][answerOrder[2]])) {
HandleAnswer(answerOrder[2]);
}
if (GUI.Button(new Rect(780, 250, 100, 30), questions[0][answerOrder[3]])) {
HandleAnswer(answerOrder[3]);
}
}
}
private void HandleAnswer(int answer) {
if (answer == 1) {
NextQuestion();
answerOrder.Add(3);
answerOrder.Add(2);
}
else {
}
}
void OnGUI() {
if (GUI.Button(new Rect(300, 250, 100, 30),"50/50")) {
answerOrder.Remove(3);
answerOrder.Remove(2);
}
if(questions.Count > 0) {
DrawInfo();
}
}
void Start() {
questions.Add(new string[] { "A series of curved bones that are articulated with the vertebrae and occur in pairs.", "Rib", "Radius", "Scapular", "Skull"});
questions.Add(new string[] { "What is the part of the skull that encloses the brain?", "Cranium", "Coccyx", "Carpals", "Clavicle" });
questions.Add(new string[] { "It is the bony skeleton of the head of vertebrates.", "Skull", "Face", "Ulna", "Tibia"});
questions.Add(new string[] { "What is the longest, and strongest bone in the human body that extends from the pelvis to the knee?", "Femur", "Fibula", "Metacarpals", "Humerus"});
questions.Add(new string[] { "Which of these is the bone of the fingers or toes?", "Phalanges", "Sacrum", "Scapular", "Carpals"});
questions.Add(new string[] { "It is the bones of the wrist.", "Carpals", "Clavicle", "Coccyx", "Cranium"});
Shuffle(questions);
Shuffle(answerOrder);
}
void NextQuestion() {
questions.RemoveAt(0);
Shuffle(answerOrder);
}
static readonly System.Random rng = new System.Random();
public static void Shuffle<T>(IList<T> list) {
int n = list.Count;
while(n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
Answer by smoggach · Aug 14, 2014 at 01:33 PM
This is confusing... first you say your question is "how to get it back after answering" then you say the issue is that it keeps adding after you answer correctly?
Create a boolean variable to keep track of whether they pressed 50/50. Set it to true when they press it and you remove the answers. Then where you are currently re-adding them you check that boolean. If it's true you can re-add them, otherwise do nothing. Then set the bool to false again. This strategy is called using a "flag" and is pretty useful for distinguishing between states.
i already tried that but the problem is it keeps on looping when i add it back
The only loop here that I can see is when shuffling. Tried re-adding BEFORE calling NextQuestion?
yes
if (answer == 1) {
NextQuestion();
for(int i = 0; i < 1; i++){
add = true;
}
}
else {
}
}
if (add){
foreach(int item in answerOrder){
answerOrder.Add(2);
answerOrder.Add(3);
}
}
if (remove){
foreach(int item in answerOrder){
answerOrder.Remove(2);
answerOrder.Remove(3);
print (item);
}
}
if (GUI.Button(new Rect(300, 250, 100, 30),"50/50")) {
remove=true;
}
and this is what happens
you're still calling NextQuestion before everything else. Then you're removing from answerOrder WHILE you iterate through it...
yes because when i do it like this
private void HandleAnswer(int answer) {
if (answer == 1) {
for(int i = 0; i < 1 ; i++){
add = true;
}
NextQuestion();
}
else {
}
}
this is what happens
it wont stop adding
Your answer
![](https://koobas.hobune.stream/wayback/20220613160549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
instantiate problem help? 1 Answer
Trouble with targeting enemies from a List 1 Answer
How to remove objects from a list ? 3 Answers
Remove and Add to List By Name aad 1 Answer