Deleting data after user played the game, not DontDestroyOnLoad

Greetings guys! I have a quick question :D. I am currently trying to develop an educational game. Where the “teacher” sets up his question and answers and in the next scenes the “user” would have to guess the question. At the moment I am using DontDestroyOnLoad command which is not so good because I can’t delete the questions unless I quit the game. What should i use so i can delete the questions after playing it once ? and let the data persist throughout the gameplay and scenes. Thank you :smiley:

This was to long to be a comment, so I’ll post it as an answer, which I believe it is. At least an example of how to solve your problem (If I understood you correctly)

I think we’ll need to see more of how this project is setup in order to help you. If the object is destroy, then it would not be accessible again, that’s correct. How is this object used? Are you creating a new object for every question?

Since the only thing you keep in these objects, it would make sense to keep all questions in the same object, and simply manipulate the questions and never delete that object.

Here’s another approach that I just whipped together. Haven’t tried it though, but it should work:

using System.Collections.Generic;
using System.Linq;

public class QuestionManager
{
    //Singleton pattern. 
    private static QuestionManager instance;
    public static QuestionManager Instance
    {
        get
        {
            if(instance == null)
            {
                instance = new QuestionManager();
            }
            return instance;
        }
    }

    private Dictionary<string, string> questionsAndAnswers = new Dictionary<string, string>();


    /// <summary>
    /// Remove a question from the dictionary.
    /// Will return true if successfull, and false if the question did not exist to begin with.
    /// </summary>
    /// <param name="question"></param>
    /// <returns>true if question was removed, false otherwise</returns>
    public bool RemoveQuestion(string question)
    {
        if (instance.questionsAndAnswers.ContainsKey(question))
        {
            instance.questionsAndAnswers.Remove(question);
            return true;
        }
        return false;
    }

    /// <summary>
    /// Returns all the questions currently stored as an array.
    /// </summary>
    /// <returns>A string array of all questions</returns>
    public string[] GetAllQuestions()
    {
        return instance.questionsAndAnswers.Keys.ToArray();
    }

    /// <summary>
    /// Add a new question. Supply the answer and the question.
    /// </summary>
    /// <param name="question"></param>
    /// <param name="answer"></param>
    public void AddQuestion(string question, string answer)
    {
        instance.questionsAndAnswers.Add(question, answer);
    }


    /// <summary>
    /// Get answer for a specific question. If this question does not exist, this will return null.
    /// Otherwise, it will return the answer to the question.
    /// </summary>
    /// <param name="question"></param>
    /// <returns>The answer (if any), otherwise null</returns>
    public string GetAnswer(string question)
    {
        if (instance.questionsAndAnswers.ContainsKey(question))
        {
            return instance.questionsAndAnswers[question];
        }
        return null;
    }

}

Note that this is not deriving from MonoBehaviour, and that it’s static with a singleton pattern. This means that you don’t have to add it to any game object, and the whole problem with dontDestroyOnLoad is gone. Nothing is destroyed here.

Use it like this, from anywhere in any script and any scene. The questions will persist between scene changes:

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

public class QuestionTester : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string testQuestion = "Where do dolphins live?";
        string answer = "In the ocean";
        

        QuestionManager.Instance.AddQuestion(testQuestion, answer);

        string testAnswer = "On Land";

        if(QuestionManager.Instance.GetAnswer(testQuestion) == testAnswer)
        {
            Debug.Log("Correct answer!");
        }
        else
        {
            Debug.Log("Wrong! Correct answer is: " + QuestionManager.Instance.GetAnswer(testQuestion));
        }

        //To remove a question when you're done, just do:
        QuestionManager.Instance.RemoveQuestion(testQuestion);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Edit:
Not that this all questions and answers will be case sensitive here.