Hi there, have a nice day, my game which consists of a visual novel need read the dialogues, for that I created text files and put them in the Resources folder,
each txt represents the dialogue that goes on every scene
Dialogue1 = Scene1
Dialogue 2 = Scene2 etc etc etc
to call the dialogs I Use TextAssets and Resources.load and told him to find a text file called only “Dialogue”, then I converted the textAsset in a string and I created another string called SceneNum, in this string was the number current scene to finally add Dialogue + SceneNum and the result was the dialogue that belonged to that scene and find it
Example : Dialogue1
when the game is running appears an error in the line 6 string file = mydata.text; saying NullReferenceException: Object reference not set to an instance of an object
I do not know what I’m failing, I would greatly appreciate your help
void Start()
{
//finding the dialogue
TextAsset mydata = Resources.Load("Dialogue") as TextAsset;
//converting TextAsset to string
string file = mydata.text;
//number of the current scene
string sceneNum = SceneManager.GetActiveScene().name;
sceneNum = Regex.Replace(sceneNum, "[^0-9]", "");
//add "Dialogue" with the number of the current scene that
//results in the dialogue that belongs to that scene
file += sceneNum;
file += ".txt";
lines = new List<DialogueLine>();
LoadDialogue(file);
}
Resources.Load loads the file itself. So what you’re currently doing is loading the contents of a file called “Dialogue.txt”, appending a “3.txt” (for example) to those contents, and then calling LoadDialogue (which does what?) on that.
What you want is probably:
TextAsset myData = Resources.Load(“Dialogue” + sceneNum) as TextAsset;
string contents = myData.text; // Has the contents of that file
I’m not sure why you felt the need to make two post about the same problem. But what Errorsatz said is correct. You’re telling it to load a file called Dialogue, but that file doesn’t exist. You need to add the number to the Word Dialogue. He gave the perfect example of how to fix it.
in the Debug Log showed 2 of the 6 lines of dialogue, at least that indicated he found the Dialogue1, but then this error appeared ArgumentException: path contains invalid characters It refers to StreemReader which reads each line of text and displayed , StreamReader belongs to the function LoadDialogue, th error is on the line 15 LoadDialogue(file) and on the line 4 StreamReader r = new StreamReader(filename)
public void LoadDialogue(string filename)
{
string line;
StreamReader r = new StreamReader(filename);
using (r)
{
do
{
line = r.ReadLine();
if (line != null)
{
string[] lineData = line.Split(';');
if (lineData[0] == "Player")
{
DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, "");
lineEntry.options = new string[lineData.Length - 1];
for (int i = 1; i < lineData.Length; i++)
{
lineEntry.options[i - 1] = lineData[i];
}
lines.Add(lineEntry);
}
else {
DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), lineData[3]);
lines.Add(lineEntry);
}
Resource.Load loads your data into the TextAsset. You then access the text with mydata.text or mydata.tostring(). The reason streamreader isn’t working is you’re passing it the data from your text file and telling it to use that data like a file path to a file.
However, you have already loaded that data, so you’ll just have to loop through your string depending on how it is laid out.
Another option is to use Json, so you can just cast it into a class with dialogue variables set up, that would be easier than your current setup. Json has a relatively low learning curve for the simple stuff like this.