如何使用脚本更改按钮上的文本?

问题描述 投票:0回答:1

我对 Unity 相当陌生,我正在尝试制作一个脚本来更改 firebase 上数据中的按钮文本。在画布上,我有一个文本 (TMP) 对象和四个按钮(每个按钮都有 TMP 子项)。我的脚本确实从 firebase 中提取数据(我使用调试语句进行了测试)。我能够获取画布上的 Text 对象,以根据从 firebase 提取的数据更新其文本,但是即使我使用相同的方法,按钮上的 Text 子项也不会更改。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;         // ui components
using TMPro;                  // Add the TextMeshPro namespace
using Firebase.Database;      // database operations

public class questionLoader : MonoBehaviour
{
    public TextMeshProUGUI questionText;

    public Button choiceAButton;
    public Button choiceBButton;
    public Button choiceCButton;
    public Button choiceDButton;

    private DatabaseReference dbReference;

    void Start()
    {
        // Initialize Firebase database reference
        dbReference = FirebaseDatabase.DefaultInstance.RootReference;

        // Load the first question to test
        LoadQuestion("01");
    }

    void LoadQuestion(string questionID)
    {
        Debug.Log($"Loading question with ID: {questionID}");
        dbReference.Child("questions").Child(questionID).GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted)
            {
                Debug.LogError("firebase data not retrieved");
            }
            else if (task.IsCompleted)
            {
                DataSnapshot snapshot = task.Result;

                if (snapshot.Exists)
                {
                    Debug.Log("IN SNAPSHOT IF STATEMENT");

                    // Set question text
                    string question = snapshot.Child("question").Value.ToString();
                    Debug.Log($"Question: {question}");


                    // Check if choice nodes exist
                    bool choiceAExists = snapshot.Child("choiceA").Value != null;
                    bool choiceBExists = snapshot.Child("choiceB").Value != null;
                    bool choiceCExists = snapshot.Child("choiceC").Value != null;
                    bool choiceDExists = snapshot.Child("choiceD").Value != null;

                    Debug.Log($"Choice A Exists: {choiceAExists}");
                    Debug.Log($"Choice B Exists: {choiceBExists}");
                    Debug.Log($"Choice C Exists: {choiceCExists}");
                    Debug.Log($"Choice D Exists: {choiceDExists}");

                    // Assign database values to strings
                    string choiceA = snapshot.Child("choiceA").Value != null ? snapshot.Child("choiceA").Value.ToString() : "No Choice A";
                    string choiceB = snapshot.Child("choiceB").Value != null ? snapshot.Child("choiceB").Value.ToString() : "No Choice B";
                    string choiceC = snapshot.Child("choiceC").Value != null ? snapshot.Child("choiceC").Value.ToString() : "No Choice C";
                    string choiceD = snapshot.Child("choiceD").Value != null ? snapshot.Child("choiceD").Value.ToString() : "No Choice D";


                    // see if data was successfully stored from database

                    Debug.Log($"Choice A: {choiceA}");
                    Debug.Log($"Choice B: {choiceB}");
                    Debug.Log($"Choice C: {choiceC}");
                    Debug.Log($"Choice D: {choiceD}");

                    // Update question text using TextMeshProUGUI
                    questionText.text = question;
                    choiceAButton.GetComponentInChildren<TextMeshProUGUI>().text = choiceA;
                    choiceBButton.GetComponentInChildren<TextMeshProUGUI>().text = choiceB;
                    choiceCButton.GetComponentInChildren<TextMeshProUGUI>().text = choiceC;
                    choiceDButton.GetComponentInChildren<TextMeshProUGUI>().text = choiceD;
             

                    Debug.Log("Question and choices updated");
                }
                else
                {
                    Debug.LogWarning("No question data found in Firebase for the ID");
                }
            }
        });
    }

}

调试语句都显示了数据库中值的正确分配。我尝试放置一个变量来引用按钮的 TextMeshProUGUI 组件,如下所示:

private TextMeshProUGUI choiceAText;

在 LoadQuestion 函数内部使用它来分配一个值,如问题文本。

choiceAtext.text = choiceA;
但这也不起作用。

我尝试将更改硬编码到启动函数中按钮的文本子级,并且这可以更改文本,所以我假设问题在于我如何访问函数内的子级。

任何帮助表示赞赏!谢谢你:)

c# unity-game-engine button game-development
1个回答
0
投票

我手动输入了一些数据并且代码可以工作。 First picture in editor Second picture after starting the simulation

检查画布中是否有该按钮。 Buttons as children of canvas

检查您是否安装了 TMP 软件包。 Packages

如果您完成了所有这些操作并通过 UI -> Button - Text Mesh Pro 创建了按钮;代码可以工作,否则你的数据库有问题,但如果你说调试显示了正确的值分配,那么问题就出在按钮游戏对象的创建上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;         // ui components
using TMPro;                  // Add the TextMeshPro namespace

public class questionLoader : MonoBehaviour
{
    public TextMeshProUGUI questionText;

    public Button choiceAButton;
    public Button choiceBButton;
    public Button choiceCButton;
    public Button choiceDButton;

    private string[] dbReference;

    void Start()
    {
        // Initialize Firebase database reference
        dbReference = new string[4];
        dbReference[0] = "A";
        dbReference[1] = "B";
        dbReference[2] = "C";
        dbReference[3] = "D";

        // Load the first question to test
        LoadQuestion("01");
    }

    void LoadQuestion(string questionID)
    {
        Debug.Log($"Loading question with ID: {questionID}");


                    // Set question text
                    string question = "????????";
                    Debug.Log($"Question: {question}");


                    // Check if choice nodes exist
                    bool choiceAExists = dbReference[0] != null;
                    bool choiceBExists = dbReference[1] != null;
                    bool choiceCExists = dbReference[2] != null;
                    bool choiceDExists = dbReference[3] != null;

                    Debug.Log($"Choice A Exists: {choiceAExists}");
                    Debug.Log($"Choice B Exists: {choiceBExists}");
                    Debug.Log($"Choice C Exists: {choiceCExists}");
                    Debug.Log($"Choice D Exists: {choiceDExists}");

                    // see the data stored
                    Debug.Log($"Choice A: {dbReference[0]}");
                    Debug.Log($"Choice B: {dbReference[1]}");
                    Debug.Log($"Choice C: {dbReference[2]}");
                    Debug.Log($"Choice D: {dbReference[3]}");

                    // Update question text using TextMeshProUGUI
                    choiceAButton.GetComponentInChildren<TextMeshProUGUI>().text = dbReference[0];
                    choiceBButton.GetComponentInChildren<TextMeshProUGUI>().text = dbReference[1];
                    choiceCButton.GetComponentInChildren<TextMeshProUGUI>().text = dbReference[2];
                    choiceDButton.GetComponentInChildren<TextMeshProUGUI>().text = dbReference[3];


                    Debug.Log("Question and choices updated");
                
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.