# Do a quiz from web api data

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
{
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();
}
}
}

• And what is your exact problem? May 6 at 16:35
• 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) May 6 at 16:43

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

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.

• 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(); } } May 6 at 18:41

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;