- Home /
Writing to txt file using textfield problem
Hallo everybody,
I'm trying to store written Strings from a TextField into an external file (.txt file) and then read those strings from the .txt file and load them into an Array for in game usage. While the reading from file function works perfectly i can't seem to make the write function work. When i build the scene with the textField contained i get an NullReferenceException, but i just can't seem to find why. Here is my code:
manualKeyboard Script:
private var currentWordLabel: wordControl;
var stringToEdit : String;
var myStyle : GUIStyle;
var delay = 0;
function Start ()
{
currentWordLabel = FindObjectOfType(wordControl);
}
function OnGUI () {
stringToEdit = GUI.TextField(Rect(100,100,200,20), stringToEdit, 25, myStyle);
//if (currentWordLabel != null)
//{
currentWordLabel.AddToFile(stringToEdit);
//}
}
wordControl script:
import System.IO;
var sr : StreamReader;
var sw : StreamWriter;
var wordArray : String[];
var currentWord :String;
var maxAmount : int = 30;
var currentAmount : int = 0;
function Start () {}
function changeWord () {
var randomInt = Random.Range(0,currentAmount);
currentWord = wordArray[randomInt];
guiText.material.color = Color.red;
guiText.text = currentWord;
}
function LoadFile () {
try {
// Create an instance of StreamReader to read from a file.
sr = new StreamReader("words_english.txt");
// Count amount of words in file
currentAmount = 0;
while (sr.ReadLine() != null && currentAmount < maxAmount) {
currentAmount++;
}
Debug.Log("Size is:" + currentAmount.ToString());
wordArray = new String[currentAmount];
sr = new StreamReader("words_english.txt");
// Fill array with words from file
var i : int = 0;
var line : String = sr.ReadLine();
while (line != null && i < maxAmount) {
wordArray[i] = line;
Debug.Log("Added word: " + line);
line = sr.ReadLine();
i++;
}
sr.Close();
}
catch (e) {
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
}
function AddToFile (word : String) {
try {
// Create an instance of StreamWriter to write text to a file.
sw = new StreamWriter("words_english.txt");
// Add some text to the file.
//sw.Write("The date is: ");
sw.WriteLine(word);
sw.Close();
}
catch (e) {
// Let the user know what went wrong.
print("File coudn't be accessed:");
print(e.Message);
}
LoadFile();
}
Am i doing something wrong or isn't it possible to write to txt files using textfields?
Thanks in advance
TextFields are just a way of manipulating string data- there is absolutely no way that it's impossible to write to a txt file just because you are using them.
Hit 'edit', select all your code, hit the 101/010 button to format your code please
I don´t know if it´s related but you could use text assets ins$$anonymous$$d of streams. If you happen to work with iOS or OSX, streams won´t work.
Why wouldn't streams work on OS X? I believe that is not true.
Sorry for the inconvinience guys, i did format it but something went wrong as it seems. Should be fixed now.
Answer by jahroy · Nov 03, 2011 at 08:29 PM
It looks like currentWordLabel could be null.
You need to make sure it's not null and that it's initialized.
Ironically it looks like you commented out the code that checks for null...
currentWordLabel = FindObjectOfType(wordControl);
if ( currentWordLabel == null ) { print("Something is wrong."); }
It is this line. The one that calls upon the wordControl class:
currentWordLabel.AddToFile(stringToEdit);
After some debugging i uncovered that the stringToEdit empty is.
if(stringToEdit == null) Debug.Log(stringToEdit);
Of course in the debug log nothing shows up.
Which is very strange since i definitely give the string a value. $$anonymous$$aybe the textfield removes that value or something?
You never check whether currentWordLabel is null... it probably is.
I just edited my answer...
CurrentwordLabel is indeed null.. but i don't understand why. I properly declare it and the string is also declared
You declare it, but you never initialize it.
Then you set it to the return value of FindObjectOfType, but never check whether or not it succeeds.
I've never used FindObjectOfType (I think it's generally a bad idea) but I believe it looks for GameObjects in the scene. $$anonymous$$y guess is you want to use a different approach to obtain and initialize your wordControl object.
It's hard to help further without knowing what kind of object a wordControl is. If it's a simple class you probably need to initialize it using a constructor. Here is some example code that shows how to add constructors to a simple class. This example creates two constructors: one without parameters and one with two parameters. $$anonymous$$y example class is declared and defined at the bottom of the script.
private var currentWordLabel : ExampleWordControl;
/* create an instance of type ExampleWordControl */
function Start ()
{
/* call the constructor that takes two parameters */
currentWordLabel = new ExampleWordControl("test", 44);
}
/* example class (class names are capitalized) */
class ExampleWordControl
{
var name : String;
var num : int;
/* example constructor */
function ExampeWordControl ()
{
name = "Untitled";
num = 0;
}
/* example constructor with parameters */
function ExampleWordControl ( someName, someNumber )
{
name = someName;
num = someNumber;
}
}
An easier way to do this (as long the wordLabel class is not complex) is to make it public and initialize it using the Inspector.
If wordLabel is a monoscript (a script that gets applied to GameObjects) it's a little different. You could use GetComponent to access another object OR you could make currentWordLabel public and drag and drop a GameObject with a wordControl script attached to it onto your script in the inspector.
Hope some of this makes sense...
Answer by SteveOxen268 · Sep 21, 2013 at 09:34 PM
Work with word files by the use of fix documents word
Learn given item http://www.fixdoconline.fixdocfiles.com/
Your answer
![](https://koobas.hobune.stream/wayback/20220613031612im_/https://answers.unity.com/themes/thub/images/avi.jpg)