0
\$\begingroup\$

i'm new to unity and i want to do a quiz using data coming from a web api

I was able to retreive data from the api but i couldn't make the game logic with them

This is where i arrived

    private IEnumerator GetRequest()
{

    UnityWebRequest response = UnityWebRequest.Get(apiUrl);

    yield return response.SendWebRequest();

    if (response.result == UnityWebRequest.Result.ProtocolError)
    {
        response = null;
    }

    else if (response.result == UnityWebRequest.Result.ConnectionError)
    {
        response = null;
    }
    else
    {
        var quest = JsonConvert.DeserializeObject<QuestionResponse>(response.downloadHandler.text);
        if (quest.documents.Count >= 0)
        {
            currentQuestion = Random.Range(0, quest.documents.Count);
            QuestionTxt.text = quest.documents[currentQuestion].fields.q.stringValue;
            Ch1Txt.text = quest.documents[currentQuestion].fields.ch1.stringValue;
            Ch2Txt.text = quest.documents[currentQuestion].fields.ch2.stringValue;
            Ch3Txt.text = quest.documents[currentQuestion].fields.ch3.stringValue;
            Ch4Txt.text = quest.documents[currentQuestion].fields.ch4.stringValue;
        }
        else
        {
            GameOver();
        }
        }
    }
\$\endgroup\$
2
  • 2
    \$\begingroup\$ And what is your exact problem? \$\endgroup\$
    – Zibelas
    May 6 at 16:35
  • \$\begingroup\$ Now i want to know how to write a script that allows me when i select one of the correct answers to register it as correct and if not as false (I already have a script for correct and false i just didn't know how to assign them to the Ch1.text for example) \$\endgroup\$
    – Jack Hail
    May 6 at 16:43

2 Answers 2

0
\$\begingroup\$

Here is a rough breakdown how I would make a quiz game.

First divide the responsibility. Currently your request that gets the data is as well handling the display. To adapt it easier, I would first create a GameManager, GameLogic or however you want to name it. This should handle

GameManager

  • Holding the questions
  • Knows which answer is right
  • How many question were right/ wrong

UIManager, DisplayManager, etc

  • User can choose an answer
  • User gets displayed right/ wrong answer
  • Pointdisplay

DataManager

  • Loads questions

The UIManager does not know which answer is right or wrong. It just tells your GameManager: The Player picked Answer 1. Now it is the responsibility of the GameManager to check if that is right or wrong and just tells the UIManager back: That was correct. Or wrong. Your GameManager does not care how you want to display this to the player. Your DataManager is just loading the questions. As you have done this already with a WebService, nothing to add. But they could be as well just stored in a json file on your device. Or a database. Later you can swap the DataManager for something else and just pass the questions to the GameManager. This means, the GameManager does not care at all from where your questions are coming. It just wants the questions.

If you plan your game ahead, it will make it easier to make changes later as well. Change how the questions should be displayed? Your UIManager is the correct place. Nothing needs to be done on the actual logic. You have new features? Like a 50/ 50 joker etc. Adding to the GameManager and handle the logic there, telling the UIManager what should be removed.

This can be later made than as well easily in a network game where your GameManager is on your server and the player can't just cheat with seeing the answer in your UIManager. Or how you get the questions. Or from where.

\$\endgroup\$
1
  • \$\begingroup\$ Ok thank you i get that but i'm trying to make a very simple quiz game I do have an answer script where it has the logic when the answer is correct and false public void Answer() { if (isCorrect) { GetComponent<Image>().color = Color.green; Debug.Log("Correct Answer"); theQuiz.correct(); } else { GetComponent<Image>().color = Color.red; Debug.Log("Wrong Answer"); theQuiz.wrong(); } } \$\endgroup\$
    – Jack Hail
    May 6 at 18:41
0
\$\begingroup\$

This is my answer script, the thing that i want to do is to relate both together

  public void Answer()
{
    if (isCorrect)
    {
        GetComponent<Image>().color = Color.green;
        Debug.Log("Correct Answer");
        theQuiz.correct();
    }
    else
    {
        GetComponent<Image>().color = Color.red;
        Debug.Log("Wrong Answer");
        theQuiz.wrong();
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ And from where is isCorrect coming? \$\endgroup\$
    – Zibelas
    May 6 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .