The code read the text and returns a random string, but I want it not to show the same string again. Is there a way to do it? I tried several ways but I am quite new so I could not find a way to do it with return. Thank you Here is my code :
using UnityEngine;
using System.Collections;
using System.IO; // For parsing text file, StringReader
using System.Collections.Generic; // For List
using UnityEngine.UI;
using System.Linq;
public class FileIO : MonoBehaviour
{
public Transform contentWindow;
public GameObject recallTextObject;
public TextAsset wordfile; // Text file (assigned from Editor)
public List<string> lineList = new List<string>(); // List to hold all the lines read from the text
file
public string abc { get; private set; }
void Start()
{
ReadWordList();
recallTextObject.GetComponent<Text>().text = GetRandomLine();
Instantiate(recallTextObject, contentWindow);
}
public void ReadWordList()
{
// Check if file exists before reading
if (wordfile)
{
string line;
StringReader textStream = new StringReader(wordfile.text);
while ((line = textStream.ReadLine()) != null)
{
// Read each line from text file and add into list
lineList.Add(line);
}
textStream.Close();
}
}
public string GetRandomLine()
{
// Returns random line from list
string abc = lineList[Random.Range(0, lineList.Count)];
return abc;
}
Aucun commentaire:
Enregistrer un commentaire