Getting quiz data from firebase to unity

Hello everyone,

I have created a quiz game and now i want to store the quiz data like questions, options and correct answers etc. I have stored few dummy questions in firebase and I’m able to read the key value but when i console it using debug statement, inside console its showing Debug.log(Object)(debug is to check the result) but i want to get the data and merge with my QuizManager script(Right now its getting data from scriptable object have to change it to server). Kindly go through the attached files so that you will get exactly what I’m doing.
Any help would be appreciated.


Thanks in advance.

And one more thing i have used rest client to get the data from firebase.

5801548–613315–QuizManager.cs (1.38 KB)
5801548–613318–Scriptable.cs (246 Bytes)

Do you have a specific question about this? What specifically is going wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuizManager : MonoBehaviour
{
    [SerializeField] private QuizUI quizUI;

    [SerializeField]
    private Scriptable quizData;
    private List<Question> questions;
    private Question selectedQuestions;
    // Start is called before the first frame update
    void Start()
    {

        questions = quizData.questions;
        SelectQuestion();
    }

   
    void SelectQuestion()
    {
        int val = Random.Range(0, questions.Count);
        selectedQuestions = questions[val];

        quizUI.SetQuestion(selectedQuestions);
    }

    public bool Answwer(string answered)
    {
        bool correctAns = false;
        if (answered == selectedQuestions.correctAns)
        {
            correctAns = true;
        }
        else
        {
            //No
        }

        Invoke("SelectQuestion", 0.4f);
        return correctAns;
    }
}

[System.Serializable]
public class Question
{
    public string questionInfo;
    public QuestionType questionType;
    public Sprite questionImg;
    public AudioClip questionClip;
    public UnityEngine.Video.VideoClip questionVideo;
    public List<string> option;
    public string correctAns;
}

[System.Serializable]
public enum QuestionType
{
    TEXT,
    IMAGE,
    VIDEO,
    AUDIO
}

i want to how to get the data from the firebase to unity so that i can replace the scriptable object with that

Have you gone through Google’s tutorial? Get Started with Firebase Realtime Database for Unity

I used Rest Client plugin from asset store, now am able to get the data but am not bale to display display it in my gameplay. Earlier i was using scriptable data to store my questions now i have to change it with the questions which i’m getting from server.

I’m managing the data display using QuizManager.cs and getting the data from server using PlayerScore.cs. Kindly have a look into those scripts and if possible help me out on how to take the PlayerScore.cs data to QuizManager.cs so that i can display the question. Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuizManager : MonoBehaviour
{
    [SerializeField] private QuizUI quizUI;

    [SerializeField]
    private JsonData quizData;
    private List<qList> questions;
    private qList selectedQuestions;
    private PlayerScore playerScore;
    // Start is called before the first frame update
    void Start()
    {
      
        questions = quizData.data;
        SelectQuestion();
    }

   
    void SelectQuestion()
    {
        int val = Random.Range(0, questions.Count);
        selectedQuestions = questions[val];

        quizUI.SetQuestion(selectedQuestions);
    }

    public bool Answwer(string answered)
    {
        bool correctAns = false;
        if (answered == selectedQuestions.CorrectIndex)
        {
            correctAns = true;
        }
        else
        {
            //No
        }

        Invoke("SelectQuestion", 0.4f);
        return correctAns;
    }
}

[System.Serializable]
public class Question
{
    public string questionInfo;
    public QuestionType questionType;
    public Sprite questionImg;
    //public AudioClip questionClip;
    //public UnityEngine.Video.VideoClip questionVideo;
    public List<string> option;
    public string correctAns;
}

[System.Serializable]
public enum QuestionType
{
    TEXT,
    IMAGE,
    VIDEO,
    AUDIO
}
using Proyecto26;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEditor;
using System.Linq;

public class PlayerScore : MonoBehaviour
{
    public qList list;
    public static string CorrectIndex;
    public static string question;
    public static List<string> answers;

    void Start()
    {
        GetScoreDatabase();
    }

    public void onGetScore()
    {
      
       
    }

    public void GetScoreDatabase()
    {
        RestClient.Get<JsonData>("https://fir-f65a7.firebaseio.com/questions.json/").Then(res =>
        {
            string response = JsonUtility.ToJson(res);

            JsonData jsdta = JsonUtility.FromJson<JsonData>(response);
            foreach (qList x in jsdta.data)
            {
                Debug.Log("Question" + x.question + "CorrectAnswer: " + x.CorrectIndex);
                question = x.question;
                CorrectIndex = x.CorrectIndex;
               
                for(int j=0; j<x.answers.Count; j++)
                {
                    Debug.Log("Options" + j + " :" + x.answers[j]);
                    string ans = x.answers[j];
                    answers = ans.Split(' ').ToList();
                }
            }
           
        });

    }

}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class JsonData
{
    public List<qList> data;

  
}



[System.Serializable]
public class qList
{
    public string CorrectIndex;
    public string question;
    public List<string> answers;

    public qList()
    {
        CorrectIndex = PlayerScore.CorrectIndex;
        question = PlayerScore.question;
        answers = PlayerScore.answers;
    }
}

You can use the JsonUtility.FromJsonOverwrite method to replace an existing ScriptableObject’s data with some new JSON data.

Additionally, checkout this post I made for someone else who needed help with the Firebase REST API. It’s a bit long, but hopefully it will help. You can skip the first few steps about user authentication if it’s not relevant here.

Actually I’m new to this so not able to figure out how to use the JsonUtility.FromJsonOverwrite according to my requirement.

Let’s say you have a data structure representing a quiz question…

[System.Serializable]
public struct QuizQuestion {
   public string question;

   public string answer1;
   public string answer2;
   public string answer3;
   public string answer4;

   public string correctAnswer;
}

…And you have some kind of “QuizData” ScriptableObject that acts as a container for all of your quiz questions…

public class QuizData : ScriptableObject {
   public QuizQuestion[] questions;
}

…And you add these questions to your QuizData (through the inspector or otherwise):

new QuizQuestion {
   question = "What is 2+2?",
   answer1 = "3",
   answer2 = "5",
   answer3 = "4",
   answer4 = "6",
   correctAnswer = "4"
},
new QuizQuestion {
   question = "What color is the sky?",
   answer1 = "Orange",
   answer2 = "Blue",
   answer3 = "Green",
   answer4 = "Purple",
   correctAnswer = "Blue"
},
new QuizQuestion {
   question = "What do you put in a toaster?",
   answer1 = "Toast",
   answer2 = "Forks",
   answer3 = "Onions",
   answer4 = "Bread",
   correctAnswer = "Bread"
}

You’d likely want to have some kind of “QuizDataSaver” class that’s responsible for serializing/de-serializing your QuizData to/from JSON and saving/loading it to/from your database:

public class QuizDataSaver {

   public void SaveData(QuizData quizData) {
      //Convert the QuizData to a JSON string.
      string jsonData = JsonUtility.ToJson(quizData);

      //Save the jsonData to your database using the Firebase REST API...
   }

   public void LoadData(QuizData quizData) {
      string jsonData = //Load the JSON data from your database using the REST API...

      //Overwrite a QuizData instance with the data from the database.
      JsonUtility.FromJsonOverwrite(jsonData, quizData);
   }
}

When you call the SaveData method, your QuizData object will be converted into this JSON string, which you can save to your Firebase database:

{
  "questions": [
    {
      "question": "What is 2+2?",
      "answer1": "3",
      "answer2": "5",
      "answer3": "4",
      "answer4": "6",
      "correctAnswer": "4"
    },
    {
      "question": "What color is the sky?",
      "answer1": "Orange",
      "answer2": "Blue",
      "answer3": "Green",
      "answer4": "Purple",
      "correctAnswer": "Blue"
    },
    {
      "question": "What do you put in a toaster?",
      "answer1": "Toast",
      "answer2": "Forks",
      "answer3": "Onions",
      "answer4": "Bread",
      "correctAnswer": "Bread"
    }
  ]
}

Finally, you can add a QuizData reference to your QuizManager, and use the QuizDataLoader to save/overwrite the QuizData object:

public class QuizManager : MonoBehaviour {
   public QuizData quizData;
   private QuizDataLoader quizDataLoader = new QuizDataLoader();

   //Assume once the game starts, you want to immediately load the QuizData from the database.
   void Awake() {
      //This will overwrite the QuizManager's QuizData object.
      quizDataLoader.LoadData(quizData);
   }
}
1 Like

Hey @Vryken , i have tried as you said but still i’m not able to handle the data, the questions which is there in server is not displaying over game scene, and also when i try to console the question data from main script i’m getting null as a result in console.