I’m trying to write the content of my text file to the Text of a button on the UI.
I’m writing public Text so I can plug the button Text inside the UI.
However when I’m running the code, the text on the button becomes “System.IO.StreamReader” instead of showing the real content of the usermsg.txt .
Thank you for your help!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.IO;
public class readtxtfile : MonoBehaviour {
public Text text;
void Start ()
{
StreamReader msg = new StreamReader(@"F:\Resources\usermsg.txt");
Debug.Log("reading");
text.text = msg.ToString();
msg.Close();
}
}
ToString returns the string representation of that object, you basically asked the streamreader “what are you in english?”… you need to access the stream that object is handling for you. I.e.
You created a StreamReader object and then called the standard “describe me as a string” function on it, which is .ToString(). Every C# object has that function, and all it does is describe the underlying object, generally for debugging, or simple output.
What you want is to call some kind of read operation on that object to get the underlying data out of the file that it points to, then parse the contents and transfer it to your .text field.
Instead, I suggest you use Unity’s built-in TextAsset asset type. You can put another public field in your class called TextAsset and then it will automatically be loaded and then when you want, you can transfer it to your Text UI item.
public Text text;
public TextAsset sourceFile;
void Start()
{
// transfer the contents of sourceFile to the text field.
text.text = sourceFile.text;
}
Edit: in the editor you will have to drag your text file into the sourceFile field on the object in question, just like you dragged a reference to the Text object in your UI.
I’m actually writing a txt file with in an inputfield from one scene.
In the second scene, I’m reading the txt with the TextAsset. However nothing is display because it looks like Unity did not refresh the text file. TextAsset works because it reads anything i write manually (without the first scene) in it.
I write the text file from the first scene into the Ressources folder, is that why?
If you don’t want to deal with the hassle of opening and closing streams, you can use something like this to have the stream automatically clean up after itself when exiting scope:
using (TextReader reader = File.OpenText("file.txt"))
{
reader.ReadLine();
}
So I have my inputfield storing the data into a text file… Does it mean I would need the text file data to be stored to a gameobject with the DontDestroyOnLoad script? Then add the same gameobject to the different scenes? I am confused oops.
Sorry, meant “forget this textfile madness and just have the data persist between scenes” unless there is some other reason for using the textfile. Your post read as a “I’m trying to get data from one scene to another” kind of thing to me.
Oh i’m just experimenting, thanks for helping!
I want to be able to type a message and make it display on a canvas in several different scenes. But i want to be able to read the message on this canvas after I closed and ran the software later on.
That’s why i thought about storing the text in a textfile. Sounded easy to start with
Yes, if you want the data to persist between different runs of your game, then it needs to be stored somewhere. PlayerPrefs is by far the easiest way to do this. But a text file will also work. (I often use text files in GRFON format.)
But for data that needs to pass between scenes and not persist between runs, definitely use DontDestroyOnLoad as @LeftyRighty suggested.