Код: Выделить всё
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().text = choiceA;
choiceBButton.GetComponentInChildren().text = choiceB;
choiceCButton.GetComponentInChildren().text = choiceC;
choiceDButton.GetComponentInChildren().text = choiceD;
Debug.Log("Question and choices updated");
}
else
{
Debug.LogWarning("No question data found in Firebase for the ID");
}
}
});
}
}
Код: Выделить всё
private TextMeshProUGUI choiceAText;Код: Выделить всё
choiceAtext.text = choiceA;Я попробовал жестко запрограммировать изменение текстового дочернего элемента кнопки в функции запуска, и это помогло изменить текст, поэтому я предполагаю, что проблема в том, как я получаю доступ к дочернему элементу внутри функции.
Любая помощь приветствуется! Спасибо
Подробнее здесь: https://stackoverflow.com/questions/791 ... g-a-script
Мобильная версия