So in my game basically questions are read out like a classic drinking game but when i try to access my player names list in another scene it doesnt work although there clearly are names in the list. Here is a video:
if someone fixes this for me ill send them a 10 dollar amazon gift card i promise (AUD though)
this is the first script that basically inserts the names into a list of questions stored in a csv
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
public class QuestionManager2 : MonoBehaviour
{
public string questionFilePath = "Assets/CSV/cgm.csv";
public List<string> questions = new List<string>();
public Text questionText;
private int currentQuestionIndex = 0;
private void Start()
{
// Read the CSV file and add questions to the list
using (StreamReader reader = new StreamReader(questionFilePath))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] fields = line.Split(',');
if (fields.Length > 0)
{
questions.Add(fields[0]);
}
}
}
// Display the first question
DisplayCurrentQuestion();
}
public void OnNextQuestionButtonClicked()
{
// Go to the next question in the list
currentQuestionIndex++;
// If we've reached the end of the list, loop back to the beginning
if (currentQuestionIndex >= questions.Count)
{
currentQuestionIndex = 0;
}
// Display the current question
DisplayCurrentQuestion();
}
private void DisplayCurrentQuestion()
{
if (PlayerNameManager.instance.playerNames.Count == 0)
{
Debug.LogError("No player names loaded.");
return;
}
// Replace {player} with the player names in the current question
string currentQuestion = questions[currentQuestionIndex];
foreach (string playerName in PlayerNameManager.instance.playerNames)
{
currentQuestion = Regex.Replace(currentQuestion, "{player}", playerName, RegexOptions.IgnoreCase);
}
// Update the question text UI element with the current question
questionText.text = currentQuestion;
}
}
this is my player name manager script
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class PlayerNameManager : MonoBehaviour
{
public static PlayerNameManager instance;
public List<string> playerNames;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public void AddPlayerName(string name)
{
playerNames.Add(name);
Debug.Log("Added player name: " + name);
}
}