C#中的Unity测验游戏得分

时间:2018-12-17 08:46:44

标签: c# unity3d

我正在从unity live session问答游戏教程中统一制作一个问答游戏,所有工作都很好,除了当我单击该按钮时分数不起作用时,它应该将10分数添加到Score中。这是教程:https://unity3d.com/learn/tutorials/topics/scripting/intro-and-setup?playlist=17117和我的游戏控制器代码:

    using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public class GameController : MonoBehaviour {


    public Text questionDisplayText;
    public Text scoreDisplayText;
    public Text timeRemainingDisplayText;
    public SimpleObjectPool answerButtonObjectPool;
    public Transform answerButtonParent;
    public GameObject questionDisplay;
    public GameObject roundEndDisplay;

    private DataController dataController;
    private RoundData currentRoundData;
    private QuestionData[] questionPool;

    private bool isRoundActive;
    private float timeRemaining;
    private int questionIndex;
    private int playerScore;
    private List<GameObject> answerButtonGameObjects = new List<GameObject>();

    // Use this for initialization
    void Start () 
    {
        dataController = FindObjectOfType<DataController> ();
        currentRoundData = dataController.GetCurrentRoundData ();
        questionPool = currentRoundData.questions;
        timeRemaining = currentRoundData.timeLimitInSeconds;
        UpdateTimeRemainingDisplay();

        playerScore = 0;
        questionIndex = 0;

        ShowQuestion ();
        isRoundActive = true;

    }

    private void ShowQuestion()
    {
        RemoveAnswerButtons ();
        QuestionData questionData = questionPool [questionIndex];
        questionDisplayText.text = questionData.questionText;

        for (int i = 0; i < questionData.answers.Length; i++) 
        {
            GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
            answerButtonGameObjects.Add(answerButtonGameObject);
            answerButtonGameObject.transform.SetParent(answerButtonParent);

            AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
            answerButton.Setup(questionData.answers[i]);
        }
    }

    private void RemoveAnswerButtons()
    {
        while (answerButtonGameObjects.Count > 0) 
        {
            answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
            answerButtonGameObjects.RemoveAt(0);
        }
    }

    public void AnswerButtonClicked(bool isCorrect)
    {
        if (isCorrect) 
        {
            playerScore += currentRoundData.pointsAddedForCorrectAnswer;
            scoreDisplayText.text = "Score: " + playerScore.ToString();
        }

        if (questionPool.Length > questionIndex + 1) {
            questionIndex++;
            ShowQuestion ();
        } else 
        {
            EndRound();
        }

    }

    public void EndRound()
    {
        isRoundActive = false;

        questionDisplay.SetActive (false);
        roundEndDisplay.SetActive (true);
    }

    public void ReturnToMenu()
    {
        SceneManager.LoadScene ("MenuScreen");
    }

    private void UpdateTimeRemainingDisplay()
    {
        timeRemainingDisplayText.text = "Time: " + Mathf.Round (timeRemaining).ToString ();
    }

    // Update is called once per frame
    void Update () 
    {
        if (isRoundActive) 
        {
            timeRemaining -= Time.deltaTime;
            UpdateTimeRemainingDisplay();

            if (timeRemaining <= 0f)
            {
                EndRound();
            }

        }
    }
}

和我的答案按钮代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AnswerButton : MonoBehaviour {

    public Text answerText;

    private AnswerData answerData;
    private GameController GameController;

    // Use this for initialization
    void Start () 
    {
        GameController = FindObjectOfType<GameController> ();
    }

    public void Setup(AnswerData data)
    {
        answerData = data;
        answerText.text = answerData.answerText;
    }


    public void HandleClick()
    {
        GameController.AnswerButtonClicked (answerData.isCorrect);
    }
}

和答案数据:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class AnswerData 
{
    public string answerText;
    public bool isCorrect;

}

1 个答案:

答案 0 :(得分:1)

如果一切正常(整个代码正确执行,这是我现在所假定的),则可能是您未正确设置数据。在您的游戏控制器中,有一行

playerScore += currentRoundData.pointsAddedForCorrectAnswer;
在您的AnswerButtonClicked方法中输入

,如果答案正确,则应该在分数中添加您定义的数量。由于我假设您的整个代码运行良好(我看不到您的引擎内设置,仅看到这里的代码,类似于本教程中的代码),这可能是第一个查看该错误的位置。该值可能是在Unity Inspector中或通过其他脚本设置的,因此您可能要检查其他文件或编辑器。

接下来要检查的是,按钮是否通过其事件处理程序正确链接。可以通过查看检查器来检查。在本教程系列中,此步骤是在视频末尾的点击回答部分中完成的。