1

I'm making a multiplayer quiz game and I used Scriptableobject to store my quiz data. Now I would like to change it and use Firebase but I hit the wall with it.

I Have a scriptable object to store my questions: Code (CSharp):

[CreateAssetMenu(fileName = "QuestionsTF", menuName = "QuestionTF")]
public class QuestionTF : ScriptableObject
{
    public List<Question> questionstf;
}

And Gamemanager script to handle that:

[SerializeField]
    private List<QuestionTF> quizDB;
 
    void Startgame(int index)
    {
        //int index =0;
        unAnswered = new List<Question>();
 
        for (int i = 0; i < quizDB[index].questionstf.Count; i++)
        {
            unAnswered.Add(quizDB[index].questionstf[i]);
        }
   
        SetCurrentquestion();
 
    }

I'm also using category management: Code (CSharp):

void Awake()
    {
        for (int i = 0; i < categorybtn.Count; i++)
        {
            Button localbtn = categorybtn[i];
            localbtn.onClick.AddListener(() => Click(localbtn));
        }
    }
    private void Click(Button btn)
    {
        switch (btn.name)
        {
            case "Kategoria 1":
                Startgame(0);
                Debug.Log("start");
                categoryPanel.SetActive(false);
                break;
            case "Kategoria 2":
                Startgame(1);
                categoryPanel.SetActive(false);
                break;
            case "Kategoria 3":
                Startgame(2);
                categoryPanel.SetActive(false);
                break;
        }

I've already established a connection to DB - I can add new questions but I have a problem with displaying them. Is there any way how I can directly replace ScriptableObject? I tried to use JsonUtility.From.JsonOverwritte but I cannot use JSON and list...I think It's quite easy but after so much time with this, I simply can't see the right way to do it now (Btw can I use this to also create new questions in SO? I couldn't find this information anywhere)

Thanks

OnionFan
  • 479
  • 3
  • 10
Kamild160
  • 11
  • 2
  • What you really need to do? Adding new questions from DB to QuestionTF ScriptableObject in questionstf variable? – OnionFan Jul 11 '20 at 13:44
  • Yes - that's what I would like to achieve. SO I wouldn't have to rework whole app for firebase and only replace data in Questions TF. – Kamild160 Jul 11 '20 at 13:56

1 Answers1

0

In Scriptable Objects, you can update data like in other object instances, but remember this data will keep when an object will alive.

You can update data in Scriptable Object like this:

QuestionTF Script

[CreateAssetMenu(fileName = "QuestionsTF", menuName = "QuestionTF")]
public class QuestionTF : ScriptableObject
{
    public List<Question> questionstf;

    public void AddingNewQuestions(IEnumerable<Question> questions)
    {
       questionstf.AddRange(questions);
    }
}
OnionFan
  • 479
  • 3
  • 10
  • Great thank you :)When this data will be fetched? With app start or when the SO will be used? – Kamild160 Jul 11 '20 at 15:49
  • When you add this data from another script, you need to add this data manually from DB to this object – OnionFan Jul 11 '20 at 15:52
  • Can it be done in runtime? I mean when I will add new question inside app(on one of sceneses) can I then use this question in quiz? I'm still learning about SO but from what I learned they cannot be changed when app is running. – Kamild160 Jul 12 '20 at 13:53
  • Of course, when you adding new questions in this array in runtime after that you can get these questions. – OnionFan Jul 12 '20 at 13:57