O
O
OnGBR12020-07-24 15:37:11
Unity
OnGBR1, 2020-07-24 15:37:11

Why does random have the same value?

I'm making a quiz game where you answer questions, after you've answered the next question, and so on.
I need the questions to be in random order and without repetition, for this I have a variable of type string.
I found one video where everything worked out for a person, but now it randomizes at the beginning, and then I get the same question.
Here is the video

And the code I copied

public Text texxt;

    public List<string> questions = new List<string>();
    public List<int> previousQuestion = new List<int>();

    public int questionNumber = 0;

    public static string selectedAnswer;

    public static int randQuestion = -1;
    void Start()
    {
        texxt = GetComponent<Text>();
    }

    
    void Update()
    {

        if (randQuestion == -1)
        {
            randQuestion = Random.Range(0, questions.Count);

            for(int i = 0; i < 6; i++)
            {
                if (randQuestion != previousQuestion[i])
                {

                }

                else
                {
                    randQuestion = -1;

                }
            }
        }

        if (randQuestion > -1)
        {
           //texxt.text = questions[randQuestion].ToString();
            Debug.Log(questions[randQuestion]);
            previousQuestion[questionNumber] = randQuestion;
            questionNumber++;
        }

   

    }


I already tried to do it myself, but nothing works, there is very little written on the Internet about the random string type in unity

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-07-24
@OnGBR1

on the Internet, very little is written about the random string type in unity

Because random gives a number. You are not generating a random string, but a random question number.
And why this code should somehow work is not clear at all. What do you think is algorithmically written here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question