Randomly Changing Strings?
I'm creating a trivia style game and I setup a panel with a Question Text and then another one with 4 buttons, one for each answer choice. I then setup an array that consists of a question and an array of answers with a bool for isCorrect inside of that array. Now, I can't get the buttons to change their strings to one of the answer strings in the array? Here is my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Question[] questions;
public Text questionTextUI;
public Text[] ansTextUI;
Question question;
void Start ()
{
ChooseQuestion();
}
void ChooseQuestion ()
{
//Choosing Question Text
questionTextUI.text = questions[0].questionsText[0];
//Choosing Answers Text
for (int i = 0; i < ansTextUI.Length; i ++)
{
//ansTextUI[i].text = questions[0].answers[0].answersText;
for (int o = 0; o < questions[0].answers.Length; o ++)
{
ansTextUI[i].text = questions[0].answers[o].answersText;
}
}
}
}
Question.cs:
[System.Serializable]
public class Question
{
public string[] questionsText;
public Answer[] answers;
}
Answer.cs
[System.Serializable]
public class Answer
{
public string answersText;
public bool isCorrect;
}
Answer by dhore · Dec 22, 2016 at 07:44 PM
Your Answer array variable in your Question class is called "answer" whereas on line 31 of your GameManager class you reference it as "answers" (with an s) ..typo?
Also, I don't think you need 2 for loops.. just do this:
for (int i = 0; i < ansTextUI.Length; i ++)
{
ansTextUI[i].text = questions[0].answer[i].answersText;
}
As long as ansTextUI.Length == questions[0].answers.Length
I forgot to mention I didn't have my other computer with me at the moment so I rewrote the Question and Answer class. In the actual scene there aren't any errors, it just changes all of the answer's text to the 4th index (3 since it starts at 0) in the array ins$$anonymous$$d of putting one array index with one answer, another with another, and so on until all 4 strings are on all 4 answer texts. And the length's are the same. 4 answers, and 4 strings.
Also the reason I have it looping twice is because one is for it to loop through each of the 4 buttons and the second is to loop through each of the 4 strings in the array for answers.
I didn't really explain it very well, but the code example that I gave you above is actually a fix for your for loop. You only need the 1 loop as you want to assign the values with matching indexes like so:
ansTextUI[0].text = answers[0].answersText;
ansTextUI[1].text = answers[1].answersText;
ansTextUI[2].text = answers[2].answersText;
ansTextUI[3].text = answers[3].answersText;
So just replace lines 26-33 with the code I posted above and you should be good ;)
Oh wow, that does make sense now! I figured I'd have to have a loop for each one, but that simplifies it. Thanks a lot! :) $$anonymous$$erry christmas
Your answer

Follow this Question
Related Questions
BCE0022: Cannot convert 'String' to 'int' 0 Answers
Why is my listdisplaying that an item was removed? 0 Answers
is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer
Pick a random string from a string array C# 1 Answer
Memory problem on android/ios 1GB ram with string[]? 0 Answers