0

I'm trying to do a check and run a method until it's true.

    public struct Letter
{
    public string letter;
}

private void DoStart()
{
    UsedIndexes.Clear();
    BuildGoodGame();
}

List<Letter> usedLetter = new List<Letter>();
private void BuildGoodGame()
{
    usedLetter.Clear();
    Letter temp;
    for (int i = 0; i < Couch.Length; i++)
    {
        do
        {
            couchIndex = CouchNameRandom.getRandom();
            Couch[i].setCurrentChildIndex(couchIndex);
            MisingLetterScript thisLetter = Couch[i].CurrentChild.GetComponent<MisingLetterScript>();
            string s = thisLetter.Letter;
            temp = new Letter
            {
                letter = s
            };
        } while (UsedNames.Contains(couchIndex) || usedLetter.Contains(temp));
        UsedNames.Add(couchIndex);
        usedLetter.Add(temp);
    }
}

I want to run this code twice, the first time it runs ok, it goes totally perfect. The second time, I do Invoke("DoStart", 1f) and it's no longer running, Generally Freezing Unity. The whole issue is in the returnable method, which checks if the list contains the duplicate. I do not understand why the first time works fine, then after I call again the DoStart method, it is freezing. Help!! thank you.

George
  • 45
  • 7
  • 2
    If it is freezing Unity I'm pretty sure it's running and in an endless loop. It would seem you need to debug your logic and make sure the loop will always end. – Retired Ninja Jul 25 '18 at 14:12
  • Well, it ends when I first call DoStart. – George Jul 25 '18 at 14:14
  • now I have tested, and there are cases in which even the first freeze, but there are cases in which they work in both cases. – George Jul 25 '18 at 14:20
  • these kind of bugs are best solved with a small sample (use only 10 possible random options) and use debug output `Debug.Log()` to log every possible step - each line of code – Jinjinov Jul 25 '18 at 14:25
  • put a counter in booth `do...while` and break if it reaches a threshold, like 1000, then you can treat the issue. It's probably one of them that never ends – Magnetron Jul 25 '18 at 14:31
  • does not work at all, I really can not understand why it sometimes goes, sometimes freezing – George Jul 25 '18 at 14:40
  • i tried a breakpoint, anyway nothing WTF! – George Jul 25 '18 at 14:40
  • I solved the problem by another method. I created a structure that memorizes the current letter and stores it in the list. – George Jul 25 '18 at 14:51
  • I've modified the code for those who want to see – George Jul 25 '18 at 14:51

0 Answers0