Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Cómo hacer que las preguntas se muestren una por una después de responderlas y hacer clic en el botón Reproducir

Soy nuevo en Unity e intento hacer que las preguntas se muestren una por una después de responderlas y hacer clic en el botón Reproducir

 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 answers
Answer question

0

Esto debería ser bastante sencillo.

 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(); }

Sin embargo, déjame decirte que, en general, no es una buena idea almacenar preguntas y respuestas en dos matrices individuales.

  • Realmente no puede estar seguro de que ambas matrices individuales siempre tengan la misma longitud
  • tienes doble trabajo al agregar o quitar una pregunta en el medio
  • Imagínese más tarde queriendo aleatorizar el orden de las preguntas... pierde la noción de qué respuesta pertenece a qué pregunta

Así que te sugiero que los juntes fuerte juntos en un tipo como

 [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; } }

Y ahora en su Componente, prefiere configurarlos a través del Inspector o inicializarlos a través de

 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") };

Luego, por supuesto, cambia el código en consecuencia para acceder a estos

 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(); }

Ahora, como se dijo, esto también permite darle a su aplicación un poco de aleatoriedad al mezclar las preguntas antes de comenzar:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!