Help with | Mysql database connection (random question/answer generator in textfield)

(I’ve tried everything but can’t get what I want, who is going to be my hero? <3)

I am currently making a Question / Answer game in unity. I want to put questions in a Mysql database and I want them to show up in my text field.

You should be able to put like 100 questions in a database and get a random question in your textfield. I hope you have an idea of what I mean.

I already followed a tutorial that allows me to add questions via the inspector like this:

But I don’t want to add them via the inspector, I want to add them via a database. I already connected a database but I have no idea how to code this.

Can someone please help me in the good direction or show me how to do this?

public class QuizManager : MonoBehaviour
{
    public List<QuestionAndAnswers> QnA;
    public GameObject[] options;
    public int currentQuestion;

    public Text QuestionTxt;

    private void Start()
    {
        generateQuestion();
    }


    public void correct()
    {
        //when you are right
     
        QnA.RemoveAt(currentQuestion);
        StartCoroutine(waitForNext());
    }

    public void wrong()
    {
        //when you answer wrong
        QnA.RemoveAt(currentQuestion);
        StartCoroutine(waitForNext());
    }

    IEnumerator waitForNext()
    {
        yield return new WaitForSeconds(1);
        generateQuestion();
    }

    void SetAnswers()
    {
        for (int i = 0; i < options.Length; i++)
        {
            options[i].GetComponent<AnswerScript>().isCorrect = false;
            options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];
         
            if(QnA[currentQuestion].CorrectAnswer == i+1)
            {
                options[i].GetComponent<AnswerScript>().isCorrect = true;
            }
        }
    }

    void generateQuestion()
    {
        if(QnA.Count > 0)
        {
            currentQuestion = Random.Range(0, QnA.Count);

            QuestionTxt.text = QnA[currentQuestion].Question;
            SetAnswers();
        }
        else
        {
            Debug.Log("Out of Questions");
        
        }


    }
}
[System.Serializable]
public class QuestionAndAnswers
{

    public string Question;
    public string[] Answers;
    public int CorrectAnswer;

}
public class AnswerScript : MonoBehaviour
{
    public PlayerMovement PlayerMovement;
    public bool isCorrect = false;
    public QuizManager quizManager;


    private void Start()
    {

    }

    public void Answer()
    {
        if(isCorrect)
        {
            PlayerMovement.MovePlayer();
            Debug.Log("Correct Answer");
            quizManager.correct();
        }
        else
        {
            Debug.Log("Wrong Answer");
            quizManager.wrong();
        }
    }

}

it’s in phpmyadmin

Please don’t spam multiple posts for the same single question.

What do you mean? Who is going to be your cheating hero to get your schoolwork done?!!!

Here is how to reason about code that is surprising you:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

1 Like