- Home /
I want to save my level progress in my quiz game
so this is my game, word quiz game. Every answer correct, it goes to next question. the problem is i have no idea how to save the progress of question so if i quit game or back to main menu, the question doesn't back to question #1 again. pls help me. here are the code :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class QuizManager2 : MonoBehaviour
{
public static QuizManager2 instance; //Instance to make is available in other scripts without reference
[SerializeField] private GameObject gameComplete, MainMenu, GameMenu;
[SerializeField] private Text currentQuestion;
[SerializeField] private QuizDataScriptable2 QuizDataList; //Scriptable data which store our questions data
[SerializeField] private Image questionImage; //image element to show the image
[SerializeField] private WordData[] answerWordList; //list of answers word in the game
[SerializeField] private WordData[] optionsWordList; //list of options word in the game
public Text CurrentQuestion { get { return currentQuestion; } }
private int scoreCount = 1;
private GameStatus gameStatus = GameStatus.Playing; //to keep track of game status
private char[] wordsArray = new char[18]; //array which store char of each options
private List<int> selectedWordsIndex; //list which keep track of option word index w.r.t answer word index
private int currentAnswerIndex = 0, currentQuestionIndex = 0; //index to keep track of current answer and current question
private bool correctAnswer = true; //bool to decide if answer is correct or not
private string answerWord; //string to store answer of current question
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(this.gameObject);
}
// Start is called before the first frame update
void Start()
{
scoreCount = 1;
selectedWordsIndex = new List<int>(); //create a new list at start
SetQuestion(); //set question
}
void SetQuestion()
{
gameStatus = GameStatus.Playing; //set GameStatus to playing
//set the answerWord string variable
answerWord = QuizDataList.questions[currentQuestionIndex].answer;
//set the image of question
questionImage.sprite = QuizDataList.questions[currentQuestionIndex].questionImage;
ResetQuestion(); //reset the answers and options value to orignal
selectedWordsIndex.Clear(); //clear the list for new question
Array.Clear(wordsArray, 0, wordsArray.Length); //clear the array
//add the correct char to the wordsArray
for (int i = 0; i < answerWord.Length; i++)
{
wordsArray[i] = char.ToUpper(answerWord[i]);
}
//add the dummy char to wordsArray
for (int j = answerWord.Length; j < wordsArray.Length; j++)
{
wordsArray[j] = (char)UnityEngine.Random.Range(65, 90);
}
wordsArray = ShuffleList2.ShuffleListItems<char>(wordsArray.ToList()).ToArray(); //Randomly Shuffle the words array
//set the options words Text value
for (int k = 0; k < optionsWordList.Length; k++)
{
optionsWordList[k].SetWord(wordsArray[k]);
}
}
//Method called on Reset Button click and on new question
public void ResetQuestion()
{
//activate all the answerWordList gameobject and set their word to "_"
for (int i = 0; i < answerWordList.Length; i++)
{
answerWordList[i].gameObject.SetActive(true);
answerWordList[i].SetWord('_');
}
//Now deactivate the unwanted answerWordList gameobject (object more than answer string length)
for (int i = answerWord.Length; i < answerWordList.Length; i++)
{
answerWordList[i].gameObject.SetActive(false);
}
//activate all the optionsWordList objects
for (int i = 0; i < optionsWordList.Length; i++)
{
optionsWordList[i].gameObject.SetActive(true);
}
currentAnswerIndex = 0;
}
/// <summary>
/// When we click on any options button this method is called
/// </summary>
/// <param name="value"></param>
public void SelectedOption(WordData value)
{
//if gameStatus is next or currentAnswerIndex is more or equal to answerWord length
if (gameStatus == GameStatus.Next || currentAnswerIndex >= answerWord.Length) return;
selectedWordsIndex.Add(value.transform.GetSiblingIndex()); //add the child index to selectedWordsIndex list
value.gameObject.SetActive(false); //deactivate options object
answerWordList[currentAnswerIndex].SetWord(value.wordValue); //set the answer word list
currentAnswerIndex++; //increase currentAnswerIndex
//if currentAnswerIndex is equal to answerWord length
if (currentAnswerIndex == answerWord.Length)
{
correctAnswer = true; //default value
//loop through answerWordList
for (int i = 0; i < answerWord.Length; i++)
{
//if answerWord[i] is not same as answerWordList[i].wordValue
if (char.ToUpper(answerWord[i]) != char.ToUpper(answerWordList[i].wordValue))
{
correctAnswer = false; //set it false
break; //and break from the loop
}
}
//if correctAnswer is true
if (correctAnswer)
{
scoreCount += 50;
currentQuestion.text = "Score:" + scoreCount;
Debug.Log("Correct Answer");
gameStatus = GameStatus.Next; //set the game status
currentQuestionIndex++; //increase currentQuestionIndex
//if currentQuestionIndex is less that total available questions
if (currentQuestionIndex < QuizDataList.questions.Count)
{
Invoke("SetQuestion", 0.5f); //go to next question
}
else
{
Debug.Log("Game Complete"); //else game is complete
gameComplete.SetActive(true);
}
}
}
}
public void ResetLastWord()
{
if (selectedWordsIndex.Count > 0)
{
int index = selectedWordsIndex[selectedWordsIndex.Count - 1];
optionsWordList[index].gameObject.SetActive(true);
selectedWordsIndex.RemoveAt(selectedWordsIndex.Count - 1);
currentAnswerIndex--;
answerWordList[currentAnswerIndex].SetWord('_');
}
}
}
[System.Serializable]
public class QuestionData2
{
public Sprite questionImage;
public string answer;
}
public enum GameStatus
{
Next,
Playing
}
Comment
Your answer
Follow this Question
Related Questions
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers
How to make a list of saves and load them correctly 0 Answers
Android, How can I permanently save data? (A couple variables) 4 Answers
Saving Melee Combat Template Pack (MY LAST PROBLEM) 1 Answer