Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Loop when Adding Question to List

Discussion in 'Scripting' started by RPGFabi, Feb 15, 2020.

  1. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    Hey,

    Currently I try to add an Question (Frage) to an List (fragenkatalog).

    THe whole Code works fine, just one Line adds me into a Loop so the Game freezes.
    Code (CSharp):
    1. private Fragenkatalog GenerateAbfrage(ArtDerAbfrage artDerAbfrage)
    2.     {
    3.         Fragenkatalog fragenkatalog = new Fragenkatalog();
    4.         fragenkatalog.Katalog = new List<MultipleChoiceFrage>();
    5.         int AnzahlFragen;
    6.         int[] Lernthemen;
    7.  
    8.  
    9.         if (modus == Modus.Normal)
    10.         {
    11.             Lernthemen = new int[10]{1,2,3,4,5,6,7,8,9,10};
    12.             AnzahlFragen = 40;
    13.         }
    14.         else
    15.         {
    16.             Lernthemen = new int[3] { 1, 2, 9 };
    17.             AnzahlFragen = 20;
    18.         }
    19.  
    20.         if(artDerAbfrage == ArtDerAbfrage.Alle)
    21.         {
    22.             for (int i = 0; i < AnzahlFragen; i++)
    23.             {
    24.                 bool NewAdded = false;
    25.            
    26.                 while (NewAdded == false)
    27.                 {
    28.                     int randomLerngebiet = Random.Range(0, Lernthemen.Length);
    29.                         if(randomLerngebiet == 2 && modus == Modus.Angepasst)
    30.                         {
    31.                             randomLerngebiet = 8;
    32.                         }
    33.                     int randomNumber = Random.Range(0, GameHandler.instance.GA_SortedFragen[randomLerngebiet].Count);
    34.                     MultipleChoiceFrage Frage = new MultipleChoiceFrage();
    35.                     Frage = GameHandler.instance.GA_SortedFragen[randomLerngebiet][randomNumber];
    36.  
    37.                     if (!fragenkatalog.Katalog.Contains(Frage))
    38.                     {
    39.                         Debug.Log("Fragenkatalog Count: " + fragenkatalog.Katalog.Count);
    40.                         Debug.Log("Frage: " + Frage.Question);
    41.                         Debug.Log("Anzahl Antworten: " + Frage.Antworten.Count);
    42.                         fragenkatalog.Katalog.Add(Frage);
    43.  
    44.                         NewAdded = true;
    45.                     }
    46.                     else
    47.                     {
    48.                         Debug.Log("Already Exists");
    49.                     }
    50.                 }
    51.  
    52.             }
    53.         }
    54.         return fragenkatalog;
    55.     }
    My Problem ist, that as soon as i say
    fragenkatalog.Katalog.Add(new MultipleChoiceFrage());
    it works, but adding the Question (Frage) to the List or setting it after a new Question is added to the Value of Frage it freezes.

    Here is the Code for The MultipleChoiceFrage
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Fragenkatalog
    7. {
    8.     public List<MultipleChoiceFrage> Katalog;
    9. }
    10.  
    11. [System.Serializable]
    12. public class MultipleChoiceFrage
    13. {
    14.     public string Question;
    15.     public List<Antwort> Antworten;
    16.     public string Niveau;
    17.     public int Lerngebiet;
    18.     public int Lerngebiet_2;
    19.  
    20. }
    21.  
    22. [System.Serializable]
    23. public class Antwort
    24. {
    25.     public bool IsCorrect;
    26.     public string Text;
    27. }
    28.  
    Hope someone can tell me where my Error is
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hi, please write your code in english if it's ever going to be read by anybody else. So most likely.. always. Even as a fellow german (also a Fabi^^), i find this "Denglisch" extremely hard to read and hard to follow.

    Can you pinpoint what line is last executed before the program freezes? You said that the program works if you only add a new MultipleChoiceFrage() to the list, so i assume it works if you comment out line 35, but does not as shown? If so, in the code as shown, what are the last debug.log's you can read?
    Adding something to a list should never cause a program to freeze by itself, so there is probably something other happening that causes an infinite loop, or an infinite recursive method call causing the program to freeze.

    An comparably easy way to find why a program freezes is to use a proper debugger (Visual Studio offers this). You can then set a breakpoint shortly before you think the programm freezes and step through the code. That way you see when and what starts repeating and can fix it.

    I assume you have your console open and there are no exceptions? Because at the very least you can currently run into an IndexOutOfBoundsException, since Random.Range has both the min and max as [inclusive], meaning it could return Count and thus you could try to access an array at [Count], while the last index is always Count-1.
    https://docs.unity3d.com/ScriptReference/Random.Range.html
     
    Last edited: Feb 15, 2020
  3. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    Normaly I do write in English, but since my Girlfriend is working on it too (with bad English) i needed to write it kinda in German:

    If so, in the code as shown, what are the last debug.log's you can read?
    Nothing. As my Research reached it looks like its in a loop.

    How do I use the Visual Studio Debugger with Breakpoints?

    Edit:

    This works

    Creating New Question
    Getting Data from Storage
    Asigning every Value in New Question to old one
    Adding New Question to Lis
     
    Last edited: Feb 15, 2020
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You need to attach the Unity process to your Visual Studio as described here: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html
    You should find a lot of information on using a debugger / the VS debugger, as debugging is like 80% of the coding effort haha.

    So your problem is solved?