Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created July 12, 2017 19:37
Show Gist options
  • Save unity3dcollege/cb3d061e68e35b38bb29b34948b013de to your computer and use it in GitHub Desktop.
Save unity3dcollege/cb3d061e68e35b38bb29b34948b013de to your computer and use it in GitHub Desktop.
using System.Linq;
using UnityEngine;
public class QuestionCollection : MonoBehaviour
{
private QuizQuestion[] allQuestions;
private void Awake()
{
LoadAllQuestions();
}
private void LoadAllQuestions()
{
allQuestions = Resources.LoadAll<QuizQuestion>("Questions");
}
public QuizQuestion GetUnaskedQuestion()
{
ResetQuestionsIfAllHaveBeenAsked();
var question = allQuestions
.Where(t => t.Asked == false)
.OrderBy(t => UnityEngine.Random.Range(0, int.MaxValue))
.FirstOrDefault();
question.Asked = true;
return question;
}
private void ResetQuestionsIfAllHaveBeenAsked()
{
if (allQuestions.Any(t => t.Asked == false) == false)
{
ResetQuestions();
}
}
private void ResetQuestions()
{
foreach (var question in allQuestions)
question.Asked = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment