Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Uping · Aug 14, 2014 at 12:36 AM · arraylistremove

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Uping · Aug 15, 2014 at 06:05 AM 0
Share

i already tried that but the problem is it keeps on looping when i add it back

avatar image smoggach · Aug 15, 2014 at 01:46 PM 0
Share

The only loop here that I can see is when shuffling. Tried re-adding BEFORE calling NextQuestion?

avatar image Uping · Aug 16, 2014 at 01:28 PM 0
Share

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 alt text

error.png (19.1 kB)
avatar image smoggach · Aug 16, 2014 at 03:49 PM 0
Share

you're still calling NextQuestion before everything else. Then you're removing from answerOrder WHILE you iterate through it...

avatar image Uping · Aug 16, 2014 at 04:49 PM 0
Share

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

alt text

it wont stop adding

addding.png (13.8 kB)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Emptying a Generic List / Unexpected Behaviour 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges