1
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class responder : MonoBehaviour {
private int idTema;
public Text pergunta;
public Text RespostaA;
public Text RespostaB;
public Text RespostaC;
public Text RespostaD;
public Text infoRespostas;
public string[] perguntas;
public string[] alternativaA;
public string[] alternativaB;
public string[] alternativaC;
public string[] alternativaD;
public string[] corretas;
private int idPergunta;
private float acertos;
private float questoes;
private float media;
private int notaFinal;
// Use this for initialization
void Start () {
idTema = PlayerPrefs.GetInt ("idTema");
idPergunta = 0;
questoes = perguntas.Length;
pergunta.text = perguntas [idPergunta];
RespostaA.text = alternativaA [idPergunta];
RespostaB.text = alternativaB [idPergunta];
RespostaC.text = alternativaC [idPergunta];
RespostaD.text = alternativaD [idPergunta];
infoRespostas.text = "Respondendo "+(idPergunta + 1).ToString()+ " de "+questoes.ToString()+"perguntas.";
}
public void resposta(string alternativa)
{
if (alternativa == "A")
{
if (alternativaA [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if (alternativa == "B")
{
if (alternativaB [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if (alternativa == "C")
{
if (alternativaC [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
else if (alternativa == "D")
{
if (alternativaD [idPergunta] == corretas [idPergunta])
{
acertos += 1;
}
}
proximaPergunta ();
}
void proximaPergunta()
{
idPergunta += 1;
if (idPergunta <= (questoes - 1))
{
pergunta.text = perguntas [idPergunta];
RespostaA.text = alternativaA [idPergunta];
RespostaB.text = alternativaB [idPergunta];
RespostaC.text = alternativaC [idPergunta];
RespostaD.text = alternativaD [idPergunta];
infoRespostas.text = "Respondendo " + (idPergunta + 1).ToString () + " de " + questoes.ToString () + "perguntas.";
}
else
{
media = 10 * (acertos / questoes);
notaFinal = Mathf.RoundToInt (media);
if (notaFinal > PlayerPrefs.GetInt ("notaFinal" + idTema.ToString ()))
{
PlayerPrefs.SetInt ("notaFinal" + idTema.ToString (), notaFinal);
PlayerPrefs.SetInt ("acertos" + idTema.ToString (), (int) acertos);
}
PlayerPrefs.SetInt ("notaFinalTemp" + idTema.ToString (), notaFinal);
PlayerPrefs.SetInt ("acertosTemp" + idTema.ToString (), (int) acertos);
Application.LoadLevel ("nota");
}
}
}
Where do you want to do this? Do you have any criteria? What are the questions? The question is not at all clear.
– Maniero
Hello David. Edit the question to explain your code. Explain what the questions are, what you want to do, and most importantly, where exactly is your difficulty (for example, don’t you know how to generate a random number in C#? or don’t know how to ensure that it is between 0 and the maximum of an array/list of questions? etc).
– Luiz Vieira