Answer the question
In order to leave comments, you need to log in
How to reset a number in code?
there is a code (for a game like a quiz), it turns out at the end the result is given with the number of correct answers, but when I press play again and if I do not make a single correct answer, it displays the previous result for me. How to make the variable -cor- after issuing the result reset to zero and then if you do not make a single correct one, then 0 is displayed and not the previous result
public QuestionList[] questions;
public Text reztext;
public GameObject rez;
public Text mytext;
public Text[] answerstext;
List<object> mylist;
int randq;
QuestionList crntQ;
int cor = 0;
public void start()
{
rez.SetActive(false);
}
public void OnClickPlay()
{
mylist = new List<object>(questions);
questiongenerate();
}
void questiongenerate()
{
if (mylist.Count > 0)
{
randq = Random.Range(0, mylist.Count);
crntQ = mylist[randq] as QuestionList;
mytext.text = crntQ.question;
List<string> answers = new List<string>(crntQ.answers);
for (int i = 0; i < crntQ.answers.Length; i++)
{
int rand = Random.Range(0, answers.Count);
answerstext[i].text = answers[rand];
answers.RemoveAt(rand);
}
}
else
{
rez.SetActive(true);
cor=0;
}
}
public void answersbttns(int index)
{
if (answerstext[index].text.ToString() == crntQ.answers[0])
{
cor = cor + 1;
reztext.text = "Ваш результат:" + cor + "/10";
}
mylist.RemoveAt(randq);
questiongenerate();
}
[System.Serializable]
public class QuestionList
{
public string question;
public string[] answers = new string[3];
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question