简体   繁体   English

如何在Unity中单击按钮时显示问答游戏问题?

[英]How to get quiz game questions to show when button is clicked in Unity?

I am trying to make a quiz game with four buttons for the answers and the questions are stored in an array. 我正在尝试制作一个带有四个按钮的问答游戏,并将问题存储在一个数组中。 The letters are supposed to print out one at a time and after the first 3 indexes are shown then only when a button on the screen is pressed should the next index show. 这些字母应该一次打印一个,并且在显示前三个索引之后,只有在按下屏幕上的按钮时才显示下一个索引。

So far I can only seem to get the first 3 indexes to show, but I am not sure how to get the buttons to trigger the next question in the array to show. 到目前为止,我似乎只能显示前三个索引,但是我不确定如何获取按钮来触发要显示的数组中的下一个问题。

My code is: 我的代码是:

public GameObject button1;
public GameObject button2;
public GameObject button3;
public GameObject button4;
public float letterPause = 0.05f;
public float sentencePause = 2.0f;
string[] strArray = new string[13];
int i;
int count = 3;
int score;
bool nextQuestion = false;

void Start () {
    strArray[0] = "Hello and welcome!";
    strArray[1] = "This 11 question quiz is going to test your knowledge";
    strArray[2] = "Question 1";
    strArray[3] = "Question 2";
    strArray[4] = "Question 3";
    strArray[5] = "Question 4";
    strArray[6] = "Question 5";
    strArray[7] = "Question 6";
    strArray[8] = "Question 7";
    strArray[9] = "Question 8";
    strArray[10] = "Question 9";
    strArray[11] = "Question 10";
    strArray[12] = "Question 11";
    StartCoroutine (TypeText ());
}

IEnumerator TypeText () {
    for (i = 0; i < count; i++) {
        foreach (char letter in strArray[i].ToCharArray()) {

            gameObject.GetComponent<Text> ().text += letter;
            yield return new WaitForSeconds (letterPause);
        }

        if (i != count - 1) {
        yield return new WaitForSeconds (sentencePause);
        gameObject.GetComponent<Text> ().text = "";
        }
    }
    StartCoroutine (TypeText2 ());
}

IEnumerator TypeText2 () {
    while (nextQuestion == true) {
        for (i = 3; i < strArray.Length; i++) {
            foreach (char letter in strArray[i].ToCharArray()) {

                gameObject.GetComponent<Text> ().text += letter;
                yield return new WaitForSeconds (letterPause);
            }

            gameObject.GetComponent<Text> ().text = "";
        }
    }
}

public void OnClickButton1() {
    button1.GetComponentInChildren<Text> ().text = "";
    button2.GetComponentInChildren<Text> ().text = "";
    button3.GetComponentInChildren<Text> ().text = "";
    button4.GetComponentInChildren<Text> ().text = "";
    nextQuestion = true;

}

public void OnClickButton2() {
    button1.GetComponentInChildren<Text> ().text = "";
    button2.GetComponentInChildren<Text> ().text = "";
    button3.GetComponentInChildren<Text> ().text = "";
    button4.GetComponentInChildren<Text> ().text = "";
    nextQuestion = true;
}

public void OnClickButton3() {
    button1.GetComponentInChildren<Text> ().text = "";
    button2.GetComponentInChildren<Text> ().text = "";
    button3.GetComponentInChildren<Text> ().text = "";
    button4.GetComponentInChildren<Text> ().text = "";
    nextQuestion = true;
}

public void OnClickButton4() {
    button1.GetComponentInChildren<Text> ().text = "";
    button2.GetComponentInChildren<Text> ().text = "";
    button3.GetComponentInChildren<Text> ().text = "";
    button4.GetComponentInChildren<Text> ().text = "";
    nextQuestion = true;
}

your second coroutine is not correct. 您的第二个协程不正确。 I am using count variable as position of question in array to ask and iterate using it. 我正在使用count变量作为问题在数组中的位置来询问和迭代使用它。

This is the solution: 这是解决方案:

using UnityEngine;
using System.Collections;

public class QuizScript : MonoBehaviour {
    public GameObject button1;
    public GameObject button2;
    public GameObject button3;
    public GameObject button4;
    public float letterPause = 0.05f;
    public float sentencePause = 2.0f;
    string[] strArray = new string[13];
    int i;
    int count = 3;
    int score;
    bool nextQuestion = false;

void Start () {
    strArray[0] = "Hello and welcome!";
    strArray[1] = "This 11 question quiz is going to test your knowledge";
    strArray[2] = "Question 1";
    strArray[3] = "Question 2";
    strArray[4] = "Question 3";
    strArray[5] = "Question 4";
    strArray[6] = "Question 5";
    strArray[7] = "Question 6";
    strArray[8] = "Question 7";
    strArray[9] = "Question 8";
    strArray[10] = "Question 9";
    strArray[11] = "Question 10";
    strArray[12] = "Question 11";
    StartCoroutine (TypeText ());
}

IEnumerator TypeText () {
    for (i = 0; i < count; i++) {
        foreach (char letter in strArray[i].ToCharArray()) {

            gameObject.GetComponent<text> ().text += letter;
            yield return new WaitForSeconds (letterPause);
        }

        if (i != count - 1) {
            yield return new WaitForSeconds (sentencePause);
            gameObject.GetComponent<text> ().text = "";
        }
    }
}

IEnumerator TypeText2 () {
    gameObject.GetComponent<text> ().text = "";
    if(count > strArray.Length - 1) {
        //Winninng or loosing condition 
    } else {            
        //Show the next question
        foreach (char letter in strArray[count - 1].ToCharArray()) {    
            gameObject.GetComponent<text> ().text += letter;
            yield return new WaitForSeconds (letterPause);
        }
    }
}

public void OnClickButton1() {
    button1.GetComponent<text> ().text = "";
    button2.GetComponent<text> ().text = "";
    button3.GetComponent<text> ().text = "";
    button4.GetComponent<text> ().text = "";
    count++;
    StartCoroutine (TypeText2 ());
}

public void OnClickButton2() {
    button1.GetComponent<text> ().text = "";
    button2.GetComponent<text> ().text = "";
    button3.GetComponent<text> ().text = "";
    button4.GetComponent<text> ().text = "";
    count++;
    StartCoroutine (TypeText2 ());
}

public void OnClickButton3() {
    button1.GetComponent<text> ().text = "";
    button2.GetComponent<text> ().text = "";
    button3.GetComponent<text> ().text = "";
    button4.GetComponent<text> ().text = "";
    count++;
    StartCoroutine (TypeText2 ());
}

public void OnClickButton4() {
    button1.GetComponent<text> ().text = "";
    button2.GetComponent<text> ().text = "";
    button3.GetComponent<text> ().text = "";
    button4.GetComponent<text> ().text = "";
    count++;
    StartCoroutine (TypeText2 ());
    }
}

You can further optimise your script by createing values to store the components which you are using frequently. 您可以通过创建值来存储您经常使用的组件来进一步优化脚本。 Like 喜欢

button1.GetComponent<text> ();

you can store it in variable 您可以将其存储在变量中

text button1Text = button1.GetComponent<text> ();

and use it using 并使用它

button1Text.text = "";

instead of finding the component everytime. 而不是每次都找到组件。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM