Quantcast
Channel: Questions in topic: "crash"
Viewing all articles
Browse latest Browse all 2383

Function runs fine the first time but crashes if I run it twice?

$
0
0
I have a function that runs fine the first time it executes at start but it crashes the second time I run it after an interaction. I suspect the issue may be related to for-loops similar to to the freezing described [here][1] but I think my function is just executing once. I also found [this][2] but even after 15 minutes nothing executed. Here is the function: public void ShuffleCards() { print("test"); //Choose Correct Dictionary foreach (object key in RandomKey(initialSoundDictQueue).Take(1)) { correctSoundDict = key.ToString(); } //Choose Correct Card foreach (object value in RandomValues(cardQueue).Take(1)) { correctCard = (GameObject)value; correctCard.AddComponent(); } //Reveal Correct Card foreach (object key in RandomKey(initialSoundDictQueue[correctSoundDict]).Take(1)) { string stringKey = key.ToString(); correctObject = initialSoundDictQueue[correctSoundDict][stringKey]; Instantiate(correctObject, correctCard.transform.position, Quaternion.identity, correctCard.transform.GetChild(0)); TextMeshPro title = correctCard.transform.GetChild(1).GetComponent(); string noSpaceTitle = stringKey.Replace(" ", string.Empty); title.text = noSpaceTitle; TextMeshPro subhead = correctCard.transform.GetChild(2).GetComponent(); subhead.text = stringKey; } //Choose Incorrect Dictionary foreach (object key in RandomKey(initialSoundDictQueue).Take(1)) { incorrectSoundDict = key.ToString(); } //Choose First Incorrect Card1 foreach (object value in RandomValues(cardQueue).Take(1)) { incorrectCard1 = (GameObject)value; } ; //Reveal First Incorrect Card foreach (object key in RandomKey(initialSoundDictQueue[incorrectSoundDict]).Take(1)) { string stringKey = key.ToString(); incorrectObject1 = initialSoundDictQueue[incorrectSoundDict][stringKey]; Instantiate(incorrectObject1, incorrectCard1.transform.position, Quaternion.identity, incorrectCard1.transform.GetChild(0)); TextMeshPro title = incorrectCard1.transform.GetChild(1).GetComponent(); string noSpaceTitle = stringKey.Replace(" ", string.Empty); title.text = noSpaceTitle; TextMeshPro subhead = incorrectCard1.transform.GetChild(2).GetComponent(); subhead.text = stringKey; } //Choose Second Incorrect Card1 foreach (object value in RandomValues(cardQueue).Take(1)) { incorrectCard2 = (GameObject)value; } //Reveal Second Incorrect Card foreach (object key in RandomKey(initialSoundDictQueue[incorrectSoundDict]).Take(1)) { string stringKey = key.ToString(); incorrectObject2 = initialSoundDictQueue[incorrectSoundDict][stringKey]; Instantiate(incorrectObject2, incorrectCard2.transform.position, Quaternion.identity, incorrectCard2.transform.GetChild(0)); TextMeshPro title = incorrectCard2.transform.GetChild(1).GetComponent(); string noSpaceTitle = stringKey.Replace(" ", string.Empty); title.text = noSpaceTitle; TextMeshPro subhead = incorrectCard2.transform.GetChild(2).GetComponent(); subhead.text = stringKey; } } public IEnumerable RandomValues(IDictionary dict) { System.Random rand = new System.Random(); List values = Enumerable.ToList(dict.Values); int size = dict.Count; while (true) { TValue chosenValue = values[rand.Next(size)]; if (vetList.Contains(chosenValue.ToString())) { //print("Contains"); } else { vetList.Add(chosenValue.ToString()); yield return chosenValue; } } } public IEnumerable RandomKey(IDictionary keyQueue) { System.Random rand = new System.Random(); List Keys = Enumerable.ToList(keyQueue.Keys); int size = keyQueue.Count; while (true) { TKey chosenKey = Keys[rand.Next(size)]; if (vetList.Contains(chosenKey.ToString())) { print("Contains"); } else { vetList.Add(chosenKey.ToString()); yield return chosenKey; } } } This is where it freezes when I execute resetExercise() after interaction with a card. Test will print so I assume the for loop is what's freezing it? Apologies if the answer is obvious. public void resetExercise () { GameObject ShuffleObject = GameObject.Find("InitialSoundCardShuffler"); CardShuffler = ShuffleObject.GetComponent(); CardShuffler.initialSoundCounter++; CardShuffler.progressBar.value = CardShuffler.CalculateProgress(); CardShuffler.ShuffleCards(); } [1]: https://answers.unity.com/questions/40713/how-to-use-a-while-loop-obviously-not-inside-funct.html [2]: https://answers.unity.com/questions/568762/nested-for-loop-freezing-unity-1.html

Viewing all articles
Browse latest Browse all 2383

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>