如何结束我的简单应用测验的过程?

时间:2016-08-13 18:35:33

标签: c# unity3d

请帮助我,我是c#语言的新手。我试着简单地进行应用测验,但是我遇到的问题是在最后一个测验问题之后我无法结束这个过程。 我在下面的脚本:

using UnityEngine; 
using System.Collections;
using UnityEngine.UI;
public class Quiz : MonoBehaviour { 
    [System.Serializable] 
    public class Question{ 
        public string NoSoal; 
        public string TextSoal; 
        public string Pilihan1; 
        public string Pilihan2; 
        public string Pilihan3; 
        public string Pilihan4; 
        public int qKeyAnswer; 
        public int qUserAnswer;

    } 

    public int BanyakPilihan; 
    public GUISkin mySkin; 
    private float Score; 
    public Question[] pertanyaan; 
    private GameObject coba;
    private Text nil;

    private GameObject Canvas;

    // Use this for initialization 
    void Start () { 
        BanyakPilihan = 0; 
        Score = 0; 
        Canvas=GameObject.Find("Canvas");
    } 
    // Update is called once per frame 
    void Update () { 
            } 

    void OnGUI () { 
            GUI.skin = mySkin; 
        GUILayout.BeginArea(new Rect((Screen.width/2)-200, 100 ,400, 450)); 
        GUILayout.Box(Score.ToString("F1")); 
        GUILayout.Label(pertanyaan[BanyakPilihan].TextSoal);
        if (GUILayout.Button(pertanyaan[BanyakPilihan].Pilihan1,mySkin.button)){ 
            pertanyaan[BanyakPilihan].qUserAnswer = 1; 
            CalculateResult(); 
            GoNextQuestion();

        } 
        if (GUILayout.Button(pertanyaan[BanyakPilihan].Pilihan2)){ 
            pertanyaan[BanyakPilihan].qUserAnswer = 2; 
            CalculateResult(); 
            GoNextQuestion(); 

        } 
        if (GUILayout.Button(pertanyaan[BanyakPilihan].Pilihan3)){ 
            pertanyaan[BanyakPilihan].qUserAnswer = 3; 
            CalculateResult(); 
            GoNextQuestion();

        } 

        if (GUILayout.Button(pertanyaan[BanyakPilihan].Pilihan4)){ 
            pertanyaan[BanyakPilihan].qUserAnswer = 4; 
            CalculateResult(); 
            GoNextQuestion();

        }
        GUILayout.EndArea(); 
    }

    void GoNextQuestion(){ 

        if (BanyakPilihan < pertanyaan.Length-1){ 
            BanyakPilihan++;

        }
    }       

    void CalculateResult(){ 
        float Total = 0; 
        for (int i=0; i< pertanyaan.Length; i++){ 
            if (pertanyaan[i].qKeyAnswer == pertanyaan[i].qUserAnswer){ 
                Total = Total + 1;  
            } 
        } 
        Total = Total / pertanyaan.Length * 100; 
        Score = Total; 
    } 
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

void GoNextQuestion(){ 
    GUI.skin=mySkin;
    GUILayout.BeginArea(new Rect((Screen.width/2)-200, 100 ,400, 450)); 

    if (BanyakPilihan < pertanyaan.Length-1){ 
        BanyakPilihan++;
    } else {
        MessageBox.Show("The end");
        Application.Exit();
    }
}