79128972

Date: 2024-10-26 16:26:31
Score: 1.5
Natty:
Report link

I have manually put in some data and the code works. First picture in editor Second picture after starting the simulation

Check if you have the button in canvas. Buttons as children of canvas

Check, if you have the TMP package installed. Packages

If you did all these things and created the buttons through UI -> Button - Text Mesh Pro; The code will work, otherwise there is a problem with your database, but if you said that the debugs show the right asignment of values, then the problem has to be in creation of the Button gameObject.

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");
                
    }

}
Reasons:
  • Blacklisted phrase (1): ???
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Epidermis