Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
How to make questions displayed one-by-one after answered and clicked to Play button

I am new in Unity, and try to make questions displayed one-by-one after answered and clicked to the Play button

public class PlayGame : MonoBehaviour
{
 
    public string[] questions = {"What is 10+10", "What is 20+20", "What is 30+30", "What is 40+40", "What is 50+50"};
    public string[] correctAnswer = {"20", "40", "60", "80" , "100"};

    public Text question;
    public InputField answer;
    public int selection;


    // Start is called before the first frame update
    void Start()
    {
     question.text = questions[selection];      
    }


    public void CheckAnswer()
    {
        if (answer.text == correctAnswer.ToString())            
        {
            Debug.Log("Answer is Correct");
         //display next question
            
        }
        else
        {
            Debug.Log("Answer is not Correct");
            //display next question
        }
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

This should be quite straight forward

private int selection = -1;

void Start()
{
    ShowNextQuestion();     
}

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
    }

    question.text = questions[selection];
}

public void CheckAnswer()
{
    if (answer.text.Equas(correctAnswer[selection]))            
    {
        Debug.Log("Answer is Correct");
    }
    else
    {
        Debug.Log("Answer is not Correct");
    }

    ShowNextQuestion();
}

Let me tell you though that in general it's not a good idea to store questions and answers in two individual arrays.

  • You can't really be sure that both individual arrays always have te same length
  • you have double work when adding or removing a question in the middle
  • Imagine later wanting to randomize the order of questions .. you lose track which answer belongs to which question

So I would suggest you rather couple them strong together in a type like

[Serializable]
public class Question
{
    // Allow to edit these in the Inspector but nowhere else
    [SerializeField] private string _text;
    [SerializeField] private string _answer;

    // Other classes may only read these
    public string Text => _text;
    public string Answer => _answer;

    // Constructor 
    public Question(string text, string answer)
    {
        _text = text;
        _answer = answer;
    }
} 

And now in your Component you rather set them via the Inspector or initialize them via

public Question[] questions = {
    new Question("What is 10+10", "20"),
    new Question("What is 20+20", "40"),
    new Question("What is 30+30", "60"),
    new Question("What is 40+40", "80"),
    new Question("What is 50+50", "100")
};

Then of course you change the code accordingly to access these

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
    }

    question.text = questions[selection].Text;
}

public void CheckAnswer()
{
    if (answer.text.Equals(questions[selection].Answer))           
    {
        Debug.Log("Answer is Correct");
    }
    else
    {
        Debug.Log("Answer is not Correct");
    }

    ShowNextQuestion();
}

Now as said this allows also to give your app a bit randomness by shuffling the questions before starting:

using System.Linq;

...

private void Start()
{
    questions = questions.OrderBy(q => Random.value).ToArray();

    ShowNextQuestion();
}

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
        questions = questions.OrderBy(q => Random.value).ToArray();
    }

    question.text = questions[selection].Text;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda