You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C#保险问答游戏跳题问题排查求助

Fixing the Skipping Questions Issue in Your Unity Quiz Game

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):

  1. The first if (i == 0) block runs: you increment i to 1, update the UI to show the marriage question.
  2. Immediately after that, the code checks the next if (i == 1) block—since i is now 1, this block runs too, incrementing i to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:20:59