mardi 4 décembre 2018

How to random range can stop when the index matches the input and without duplicate question display?

In the following code I have an array to store questions. I want to make it appear random when the game starts. Using questionIndex = Random.Range(0, questionPool.Length); managed to do so, but there is still duplication and does not stop right according to the index.

void Start()
{
    audioPlayer = gameObject.AddComponent<AudioSource>();
    dataController = FindObjectOfType<DataController>();

    currentRoundData = dataController.GetCurrentRoundData();
    questionPool = currentRoundData.questions;

    timeRemaining = currentRoundData.timeLimitInSeconds;
    UpdateTimeRemainingDisplay();
    Time.timeScale = 1f;
    playerScore = 0;
    questionIndex = 0;

    ShowQuestion();
    isRoundActive = true;
}

void ShowQuestion()
{
    RemoveAnswerButtons();

    questionIndex = Random.Range(0, questionPool.Length);
    QuestionData questionData = questionPool[questionIndex];
    questionText.text = questionData.questionText;

    for (int i = 0; i < questionData.answers.Length; i++)
    {
        GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
        answerButtonGameObjects.Add(answerButtonGameObject);
        answerButtonGameObject.transform.SetParent(answerButtonParent);
        answerButtonGameObject.transform.localScale = Vector3.one;

        AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
        answerButton.SetUp(questionData.answers[i]);
    }
}




Aucun commentaire:

Enregistrer un commentaire