Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by morpheo · Jul 07, 2012 at 03:46 PM · arraysresourcestextmeshtextasset

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)
  }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges