- Home /
Why does unity expect a file when I try to put text on a mesh?
Hi. New guy here, going bald from pulling my hair with this one. Also first question!
I'm trying to load a text file according to certain settings (language, e.g) and split this file up in lines which are to be put on a mesh. This bit actually works, but when it's time to fill the mesh, Unity starts looking for a filename according to the lines in the original text file. Like so: If the text file text.txt contains the lines [0] this is a textfile [1] this is another textfile [2] this is a third one, yay! - Unity starts looking for "this is a textfile", "this is another..." etc.
As I said; fairly new to this, pls excuse any mistakes.
Code:
#pragma strict
import System.IO;
/* set up variables */
var langActive;
var txtSourceFile;
var txtMesh : GameObject;
var txtLines : String[];
var txtNarratorData : TextAsset = null;
/* variables set */
function Start () {
langActive = PlayerPrefs.GetString("gameLanguage"); // Get active/saved language from saved player preferences
txtMesh = GameObject.Find("txtContainerMesh"); // Find the gameobject that has the textmesh
FetchText(); // call FetchText() to load textfile
}
function Update () {
}
function FetchText() {
txtSourceFile = "txtNarrator"+langActive; // choose appropriate textfile according to lang-settings. no file.ext.
txtNarratorData = Resources.Load(txtSourceFile, TextAsset); // create a textasset by loading txtContent from Resources folder.
var textStream = new StreamReader(txtNarratorData.text); // fill textStream with textContent
var textFileContents = textStream.ReadToEnd(); // read the textStream until the end
textStream.Close(); // close the textStream
txtLines = textFileContents.Split("\n"[0]); // Split texlines at EOL, fill array txtLines from [0]
for (line in txtLines) // for each line in txtLines...
PutTextOnMesh(line); // call PutTextOnMesh() with line;
}
function PutTextOnMesh(txtNarrator) {
txtMesh.GetComponent(TextMesh).text = (txtNarrator); // it puts the notation/textline on the mesh!
yield WaitForSeconds(2); // wait two seconds for each line. (debug)
}
Resulting error:
FileNotFoundException: Could not find file "/Users/Fredrik/Documents/Workinprogress/Unitybuilds/Hanna&Henri/This is the English text ...and then some more English text And whaddayaknow. Even more English.". System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) System.IO.File.OpenRead (System.String path) System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) System.IO.StreamReader..ctor (System.String path) (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string) txtLoader.FetchText () (at Assets/txtLoader.js:27) txtLoader.Start () (at Assets/txtLoader.js:16)
Answer by morpheo · Jul 07, 2012 at 10:07 PM
Didn't work with while-loops or for (line in lines), etc. Rebuilt the whole thing and now it seems to work. Still don't have an answer to why it treated each line in the textfile as a filepath, but here goes... code:
#pragma strict
import System.IO;
/* set up variables */
var langActive;
var txtSourceFile;
var txtMesh : GameObject;
var txtLines : String[];
var txtLinesSub : String[];
var txtNarrator;
/* variables set */
function Start () {
langActive = PlayerPrefs.GetString("gameLanguage"); // Get active/saved language from saved player preferences
txtMesh = GameObject.Find("txtContainerMesh"); // Find the gameobject that has the textmesh
FetchText(); // call FetchText() to load textfile
}
function Update () {
}
function FetchText() {
var langActive = "Svenska"; // debug
txtSourceFile = "txtNarrator"+langActive; // choose appropriate textfile according to lang-settings. no file.ext.
var txtNarratorData = Resources.Load(txtSourceFile, TextAsset); // create a textasset by loading txtContent from Resources folder.
txtLines = txtNarratorData.text.Split("\n"[0]); // read txtNarratorData.text and split it at EOL.
for (var c = 0; c < txtLines.length; c++) { // for loop to iterate through the txtLines
var txtLineSub = txtLines[c].Split("++"[0]); // split text into subsections to fill textblock
var line = txtLineSub[0] + "\n" + txtLineSub[(txtLineSub.Length-1)]; // fill line-variable with two rows of text, always.
txtMesh.GetComponent(TextMesh).text = (line); // it puts the notation/textline on the mesh!
yield WaitForSeconds(.5); // wait two seconds for each line. (debug)
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can I code a pattern detector in an array of integers? 1 Answer
Resources.Load wont read text file 6 Answers
I have never seen a null reference exception like this one! 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer