I had a suspicion I would come across this problem. I know how to write to the file and read from it, but the following snippet isn't allowing me to input it into a text component.
public class EventHandler : MonoBehaviour
{
string _text;
Text text;
private void Awake()
{
text = FindObjectOfType<Text>();
}
public string Read(string path)
{
_text = "";
StreamReader reader = new StreamReader(path);
while (!reader.EndOfStream)
{
_text += reader.ReadLine();
}
reader.Close();
return _text;
}
public void chapter_one()
{
text.text = Read("Assets/Chapters/chapter_one.txt");
}
}
I'm sure you guys can explain why this isn't working. I already got the reference to the Text component as well.
Edit
No errors, file exists, its filled, color is white on black background, chapter_one is the file name and yes chapter_one is called, and I put Debug.Log right in the while loop and out_side of it and I received those logs. My only thoughts are that the string is not getting returned for god knows what reason, or the text component isn't appending the text.
chapter_one
is called ? Have you tried to putDebug.Log
s to find where does the problem come from ? We need more details ! \$\endgroup\$Read
function, I would have initialized_text
to the empty string, and in the loop, I would have put :_text += reader.ReadLine();
, else, you override the content of_text
each iteration. \$\endgroup\$_text
as a global variable such likestring _text
at the beginning of my class. In the Read method, I actually set it to empty string which I will update my question to reflect. The problem seems to be thattext.text
which is what is grabbing my scene's Text Component, is not receiving_text
. \$\endgroup\$