我正在Unity3d中进行一个问答游戏,我想表明玩家在哪个级别上依赖于他/她已经回答的问题。只是为了让我看看它是否有效我试图让每个问题都算作一个级别(之后我会弄清楚)。我知道我想使用哪些代码行,但我不知道在哪里放置它们才能正常工作。
这些是我想要使用的行:
levelValue = levelValue + 1;
Level.text = "Level:" + levelValue.ToString();
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Question[] questions;
private static List<Question> unansweredQuestions;
private Question currentQuestion;
public Text Level;
[SerializeField]
private Text factText;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float timeBetweenQuestions = 1f;
int i = 0;
int levelValue = 0;
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
void SetCurrentQuestion()
{
int questionIndex = i;
currentQuestion = unansweredQuestions[questionIndex];
i = i + 1;
factText.text = currentQuestion.fact;
if (currentQuestion.isTrue)
{
trueAnswerText.text = "ΣΩΣΤΟ";
falseAnswerText.text = "ΛΑΘΟΣ";
}else
{
trueAnswerText.text = "ΛΑΘΟΣ";
falseAnswerText.text = "ΣΩΣΤΟ";
}
}
IEnumerator TransitionToNextQuestion ()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue ()
{
animator.SetTrigger("True");
if (currentQuestion.isTrue)
{
Debug.Log("CORRECT!");
}else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
animator.SetTrigger("False");
if (!currentQuestion.isTrue)
{
Debug.Log("CORRECT!");
}
else
{
Debug.Log("WRONG!");
}
StartCoroutine(TransitionToNextQuestion());
}
}
答案 0 :(得分:0)
假设每个问题的代码都相同,我建议您设置
int levelValue = 0;
像这样静态:
private static int levelValue;
在C#中,未初始化的int的默认值为零。
如果您希望计数器从0开始(例如计算您已回答的问题数量),或者在脚本开头(如果您希望从1开始计算),则更改levelValue应该在更改级别之前发生(例如你现在的问题)。
在第一种情况下,它应该是这样的:
IEnumerator TransitionToNextQuestion ()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestions);
levelValue++;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
或在后一种情况下:
void Start()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
levelValue++;
SetCurrentQuestion();
}
现在关于在哪里设置Text值,您可以将它放在Unity的Update方法
中void Update(){
Level.Text= String.Format("Level: {0}",levelValue);
}
或在您的Start方法中。
还为您的代码提示: 如果要将int值增加1,请使用operator ++。 示例
i++; //THIS has the same effect as this i = i + 1;
祝您的项目顺利,如果您有任何其他信息或疑问,请随时与我联系。