Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by poken21 · Mar 12 at 03:41 PM · freezerandom.range

4 lines of code makes untiy crash

These lines of code to make random.range not repeat numbers makes unity crash after playing for a while. I removed these lines of code below and it didn't crash.

 if (RandomQuestionIndex == Pastquestion && repeat == true)
         {
             while (RandomQuestionIndex == Pastquestion)
             {
                 RandomQuestionIndex = 
                 Random.Range(0,notAnsweredQuestions.Count);
             }
         }

Whole code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 using System.Linq;
 using UnityEngine.UI;
 public class TrueOrFalseQuizManager : MonoBehaviour
 {
     private int totalquestions;
     public TextMeshProUGUI scoreText;
     private int QuestionsWrong = 0;
     public GameObject EndGameScreen;
     private bool EnteredIEnumerator;
     private bool EnteredIEnumerator2;
     public Animator CanvasAnim;
     private bool repeat = false;
     private bool End;
     public Image image;
     private int Pastquestion;
     private int RandomQuestionIndex;
     public TextMeshProUGUI tmpText;
     public QuestionHolder[] questions;
     private QuestionHolder currentQuestion;
     private  List<QuestionHolder> notAnsweredQuestions;
 
     void Start()
     {
         notAnsweredQuestions = questions.ToList<QuestionHolder>();
         RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
         Pastquestion = RandomQuestionIndex;
         currentQuestion = notAnsweredQuestions[RandomQuestionIndex];
         tmpText.text = ("" + currentQuestion.Fact);
         image.sprite = currentQuestion.QuestionImage;
     }
     private void Update()
     {
         
         if(notAnsweredQuestions.Count == 0 && End == false)
         {
             End = true;
             Debug.Log("End");
             StartCoroutine(EndQuiz(1));
         }
     }
     
     
     IEnumerator EndQuiz(float time)
     {
         
         if (EnteredIEnumerator2)
             yield break;
 
         EnteredIEnumerator2 = true;
         yield return new WaitForSeconds(time);
         //code after delay
         totalquestions = questions.Length;
         QuestionsWrong = totalquestions - QuestionsWrong;
         if(QuestionsWrong < 0)
         {
             QuestionsWrong = 0;
         }
         scoreText.text = "" + QuestionsWrong + "/" + "" + questions.Length;
         EndGameScreen.SetActive(true);
         EnteredIEnumerator2 = false;
     }
     IEnumerator NewQuestion(float time)
     {
         
             if (EnteredIEnumerator)
                 yield break;
 
             EnteredIEnumerator = true;
 
             yield return new WaitForSeconds(time);
             RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
         /*if (RandomQuestionIndex == Pastquestion && repeat == true)
         {
             while (RandomQuestionIndex == Pastquestion)
             {
                 RandomQuestionIndex = Random.Range(0, notAnsweredQuestions.Count);
             }
         }*/
         Pastquestion = RandomQuestionIndex;
         if (notAnsweredQuestions.Count > 0)
         {
             currentQuestion = notAnsweredQuestions[RandomQuestionIndex];
         }
             tmpText.text = ("" + currentQuestion.Fact);
             image.sprite = currentQuestion.QuestionImage;
             repeat = false;
             // Code to execute after the delay
 
             EnteredIEnumerator = false;
         
     }
     public void CorrectPressed()
     {
         if (currentQuestion.IsTrue == true)
         {
             Debug.Log("Yup");
             CanvasAnim.SetTrigger("True");
             if (End == false)
             {
                 notAnsweredQuestions.Remove(currentQuestion);
                 StartCoroutine(NewQuestion(1));
             }
         }
         else
         {
             Debug.Log("WRONG");
             QuestionsWrong += 1;
             CanvasAnim.SetTrigger("NOPE");
             repeat = true;
             if (End == false)
             {
                 StartCoroutine(NewQuestion(1));
             }
         }
     }
     public void FalsePressed()
     {
         if (currentQuestion.IsTrue == false)
         {
             Debug.Log("Yup");
             CanvasAnim.SetTrigger("True");
             if (End == false)
             {
                 notAnsweredQuestions.Remove(currentQuestion);
                 StartCoroutine(NewQuestion(1));
             }
             
         }
         else
         {
             QuestionsWrong += 1;
             Debug.Log("WRONG");
             CanvasAnim.SetTrigger("NOPE");
             repeat = true;
             if(End == false)
             {
                 StartCoroutine(NewQuestion(1));
             }
         }
     }
 
 }
 

alt text

ezgif-4-52343ecd7f.gif (168.2 kB)
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
0
Best Answer

Answer by Llama_w_2Ls · Mar 12 at 05:37 PM

It's because the length of notAnsweredQuestions is zero, which makes RandomQuestionIndex always equal zero (which is the same as pastQuestion), and so the loop continues forever, crashing unity.


Add a check before you run the loop to check the length of notAnsweredQuestions. Don't run the loop, if it's zero.

Comment
Add comment · Show 1 · 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 poken21 · Mar 12 at 05:44 PM 0
Share

yup that's it! Sir you're a genius

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

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whole computer tanks while starting Unity games. 0 Answers

Why Unity 5 freeze when build sharedassets0.assets ? 15 Answers

PC "crashes" when building lighting 2 Answers

How do i freeze the player for the firs 3 second? 1 Answer

Hundreds off errors n console upon starting Unity 2018.3.9f1 related to "has no meta file" 0 Answers


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