Unity脚本仅填充前两个答案文本,后两个无响应求助
Unity随机答案脚本排查:仅前两个答案显示内容
问题描述
我编写了一段Unity脚本,功能为随机抽取问题并随机分配对应答案,但当前脚本仅能填充前两个答案的TextMeshProUGUI文本,后两个答案无内容。四个答案的填充逻辑代码完全一致,现请求排查问题原因。
原脚本代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using TMPro; public class QuestionData : MonoBehaviour { public Quistions questions; public Awnser answers; [SerializeField] private TextMeshProUGUI _questionText; [SerializeField] private TextMeshProUGUI Awn1Text; [SerializeField] private TextMeshProUGUI Awn2Text; [SerializeField] private TextMeshProUGUI Awn3Text; [SerializeField] private TextMeshProUGUI Awn4Text; private int Aw1; private int Aw2; private int Aw3; private int Aw4; void Start() { AskQuestion(); } public void AskQuestion() { if (CountValidQuestions() == 0) { _questionText.text = string.Empty; ClearQuestions(); SceneManager.LoadScene("EndScreen"); return; } int randomIndex; int QNum; int Aw1 = -1; int Aw2 = -1; // Initialize Aw2 int Aw3 = -1; // Initialize Aw3 int Aw4; do { randomIndex = UnityEngine.Random.Range(0, questions.questionList.Count); QNum = UnityEngine.Random.Range(0, 4); } while (questions.questionList[randomIndex].questioned); questions.questionList[randomIndex].questioned = true; _questionText.text = questions.questionList[randomIndex].question; for (int i = 1; i <= 4; i++) { int randomAnswerIndex; do { randomAnswerIndex = UnityEngine.Random.Range(0, 4); } while (randomAnswerIndex == Aw1 || randomAnswerIndex == Aw2 || randomAnswerIndex == Aw3); int currentAnswerIndex = (randomIndex * 4) + randomAnswerIndex; foreach (var answerData in answers.awnserList) { Debug.Log("Answer: " + answerData.awnser + ", IsAwn: " + answerData.IsAwn); } switch (i) { case 1: Awn1Text.text = answers.awnserList[currentAnswerIndex].awnser; Aw1 = randomAnswerIndex; Debug.Log("Aw1: " + Aw1); break; case 2: Awn2Text.text = answers.awnserList[currentAnswerIndex].awnser; Aw2 = randomAnswerIndex; Debug.Log("Aw2: " + Aw2); break; case 3: Awn3Text.text = answers.awnserList[currentAnswerIndex].awnser; Aw3 = randomAnswerIndex; Debug.Log("Aw3: " + Aw3); break; case 4: Awn4Text.text = answers.awnserList[currentAnswerIndex].awnser; Aw4 = randomAnswerIndex; Debug.Log("Aw4: " + Aw4); break; } } } public void ClearQuestions() { foreach (var question in questions.questionList) { question.questioned = false; } } private int CountValidQuestions() { int ValidQuestions = 0; foreach (var question in questions.questionList) { if (!question.questioned) { ValidQuestions++; } } Debug.Log("Questions Left: " + ValidQuestions); return ValidQuestions; } }
注:已知“Question”和“answer”拼写错误,暂未修改。
问题排查与修复方案
1. 优先检查组件绑定(最常见原因)
- 打开Unity编辑器,找到挂载该脚本的游戏物体
- 在Inspector面板中,确认
Awn3Text和Awn4Text字段已拖拽绑定对应的TextMeshProUGUI组件,未留空 - 如果未绑定,拖拽对应组件到字段后运行测试
2. 修复随机索引逻辑的隐患
当前第4次循环时,随机生成索引可能多次尝试才命中剩余值,且存在逻辑冗余,可优化为直接获取未使用的索引,避免无效循环:
// 替换原for循环内的do-while代码块 List<int> usedIndices = new List<int>(); if (Aw1 != -1) usedIndices.Add(Aw1); if (Aw2 != -1) usedIndices.Add(Aw2); if (Aw3 != -1) usedIndices.Add(Aw3); int randomAnswerIndex = -1; for (int j = 0; j < 4; j++) { if (!usedIndices.Contains(j)) { randomAnswerIndex = j; break; } }
3. 检查答案列表长度是否匹配
- 确认
answers.awnserList的长度等于questions.questionList.Count × 4,每个问题对应4个答案 - 查看Unity控制台是否有
IndexOutOfRangeException报错,若有则补充答案列表内容
4. 清理冗余变量
类成员变量Aw1、Aw2、Aw3、Aw4未在方法外使用,且被方法内的局部变量覆盖,可删除这些成员变量,简化代码结构
修复后示例代码(关键部分优化)
public void AskQuestion() { if (CountValidQuestions() == 0) { _questionText.text = string.Empty; ClearQuestions(); SceneManager.LoadScene("EndScreen"); return; } int randomIndex; do { randomIndex = UnityEngine.Random.Range(0, questions.questionList.Count); } while (questions.questionList[randomIndex].questioned); questions.questionList[randomIndex].questioned = true; _questionText.text = questions.questionList[randomIndex].question; int usedAw1 = -1; int usedAw2 = -1; int usedAw3 = -1; for (int i = 1; i <= 4; i++) { int randomAnswerIndex = -1; List<int> usedIndices = new List<int>(); if (usedAw1 != -1) usedIndices.Add(usedAw1); if (usedAw2 != -1) usedIndices.Add(usedAw2); if (usedAw3 != -1) usedIndices.Add(usedAw3); for (int j = 0; j < 4; j++) { if (!usedIndices.Contains(j)) { randomAnswerIndex = j; break; } } int currentAnswerIndex = (randomIndex * 4) + randomAnswerIndex; switch (i) { case 1: Awn1Text.text = answers.awnserList[currentAnswerIndex].awnser; usedAw1 = randomAnswerIndex; Debug.Log("Aw1: " + usedAw1); break; case 2: Awn2Text.text = answers.awnserList[currentAnswerIndex].awnser; usedAw2 = randomAnswerIndex; Debug.Log("Aw2: " + usedAw2); break; case 3: Awn3Text.text = answers.awnserList[currentAnswerIndex].awnser; usedAw3 = randomAnswerIndex; Debug.Log("Aw3: " + usedAw3); break; case 4: Awn4Text.text = answers.awnserList[currentAnswerIndex].awnser; Debug.Log("Aw4: " + randomAnswerIndex); break; } } }
内容的提问来源于stack exchange,提问作者Ezx
相关产品推荐
相关产品推荐

