Unity C#保险问答游戏跳题问题排查求助
Hey there! Let's break down why your questions are skipping ahead—this is a super common logic misstep, so you're not alone here.
The Root Cause
Looking at your SetTextLeft and SetTextRight methods, the problem is that you're using two separate if statements instead of making them mutually exclusive. Here's what happens when you click a button for the first time (when i = 0):
- The first
if (i == 0)block runs: you incrementito 1, update the UI to show the marriage question. - Immediately after that, the code checks the next
if (i == 1)block—sinceiis now 1, this block runs too, incrementingito 2 and switching the UI to the kids question.
You're essentially doing two state updates in a single button click, skipping the i = 1 state entirely. That's why it feels like you're jumping from question 1 straight to question 3/4.
Quick Fix for Your Current Code
Change those separate if statements to if...else if so only one block runs per button click. Here's how to adjust your methods:
public void SetTextLeft(string text) { if (i == 0 ) { i++; LeftText.text = "Yes"; PlaceholderText.text = "Are you married?"; RightText.text = "No"; } else if (i == 1){ i++; PlaceholderText.text = "Do you have any kids?"; // Don't forget to update your button texts here if needed! // LeftText.text = "Yes"; // RightText.text = "No"; } } public void SetTextRight(string text) { if (i == 0) { i++; LeftText.text = "Yes"; PlaceholderText.text = "Are you married?"; RightText.text = "No"; } else if (i == 1){ i++; PlaceholderText.text = "Do you have any kids?"; // Update button texts here too if required } }
A More Scalable Approach
Since you're building a quiz, it's better to structure your questions in a reusable way instead of hardcoding each state. This makes adding new questions way easier and avoids logic bugs like this in the future. Here's a refactored version:
First, create a serializable class to hold question data (you'll be able to edit these directly in the Unity Inspector):
[System.Serializable] public class QuizQuestion { public string questionText; public string leftOption; public string rightOption; }
Then update your main script:
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ClickingButtons : MonoBehaviour { public Button LeftButton; public Button RightButton; public Text LeftText; public Text RightText; public Text PlaceholderText; // Add your questions in the Inspector! public List<QuizQuestion> quizQuestions; private int currentQuestionIndex = 0; void Start() { // Show the first question when the game starts DisplayCurrentQuestion(); } void DisplayCurrentQuestion() { if (currentQuestionIndex < quizQuestions.Count) { QuizQuestion currentQuestion = quizQuestions[currentQuestionIndex]; PlaceholderText.text = currentQuestion.questionText; LeftText.text = currentQuestion.leftOption; RightText.text = currentQuestion.rightOption; } else { // All questions are done! PlaceholderText.text = "Quiz Complete!"; LeftButton.interactable = false; RightButton.interactable = false; } } public void OnLeftButtonClicked() { // Add logic here if you need to save the left answer currentQuestionIndex++; DisplayCurrentQuestion(); } public void OnRightButtonClicked() { // Add logic here if you need to save the right answer currentQuestionIndex++; DisplayCurrentQuestion(); } }
With this setup, you just add new QuizQuestion entries in the Unity Editor, no more messing with incrementing i and nested conditionals.
内容的提问来源于stack exchange,提问作者Teej

