// QuestionManager.cs // Zweck: Lädt ein gültiges JSON aus Resources (beliebiger Name), verwaltet aktuellen Fragenindex. using UnityEngine; public class QuestionManager : MonoBehaviour { public QuestionList questionList; // geladene Fragenliste private int currentQuestionIndex = 0; // aktueller Index public static QuestionManager Instance { get; private set; } private void Awake() { // Singleton + erhalten über Szenen if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); return; } LoadQuestions(); } private void LoadQuestions() { // Alle TextAssets aus Resources durchgehen (keine feste Dateibenennung) TextAsset[] jsonFiles = Resources.LoadAll(""); bool foundValidJson = false; foreach (var file in jsonFiles) { // einfache Plausibilitätsprüfung if (file.text.TrimStart().StartsWith("{")) { try { questionList = JsonUtility.FromJson(file.text); Debug.Log($"JSON geladen: {file.name} mit {questionList.questions.Length} Fragen."); foundValidJson = true; return; // erstes gültiges JSON nehmen } catch (System.Exception ex) { Debug.LogError($"JSON-Parsing fehlgeschlagen ({file.name}): {ex.Message}"); } } } if (!foundValidJson) Debug.LogError("Kein gültiges JSON im Resources-Ordner gefunden!"); } public Question GetCurrentQuestion() { if (questionList == null || questionList.questions == null) return null; if (currentQuestionIndex < questionList.questions.Length) return questionList.questions[currentQuestionIndex]; return null; } public int GetCurrentIndex() => currentQuestionIndex; public void AdvanceToNextQuestion() => currentQuestionIndex++; public void ResetToFirstQuestion() => currentQuestionIndex = 0; }