• 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
Question by Southpaw_decoy · Jan 18, 2012 at 04:22 AM · textasset

Assigning TextAsset using script

I have a Resources folder with a bunch of .txt files in it. Each txt file is a book and the name of each file is the name of that book, except for one file which is Books.txt. It has the names of each book on a seperate line. Once the script loads I used an Awake() function to seperate the Books.txt textasset on each line into an array. Once a "select book" button is pressed, each book name from the array is used to created a series of buttons with the name of each book as the display text for that button. All of this code works.

Once the book is selected though, the script is supposed to load .txt file with the name of the book the user selected as a text asset and then split up the contents and create buttons for each chapter in the book. It doesn't work though. If I hard code a book name into the script it works, but if I use the variable of the selectedBook from the button clicked, it returns a null value. I have printed the selectedBook variable to the console before attempting to load the textasset and it seems like it should work, but it doesn't.

The problem is in the end of the code in the GetChapters() function:

 #pragma strict

var bookList : TextAsset; var guiSelectBookRect : Rect = Rect(0,0,Screen.width,40); var guiBookListRect : Rect = Rect(0,0,Screen.width,100); var bookArray = new Array(); var chapterList : Array = new Array();

 var hasSelectedBook : boolean = false;
 var selectedBook : String = "Select Book";
 var selectedChapter : String;
 var showBookList : boolean = false;
 var showChapterList : boolean = false;

 var chapterLine : String;
 var newLine = "\n"[0];
 var firstTime : boolean = true;



function Awake() { / initialized variables /

 var hasSelectedBook : boolean = false;
 var selectedBook : String = "Select Book";
 var selectedChapter : String;
 var showBookList : boolean = false;
 var showChapterList : boolean = false;
 var chapterLine : String;
 var firstTime : boolean = true;
 
 /*
     end of initialized variables
 */

 newLine = "\n"[0];
 var bookLines = bookList.text.Split(newLine);

 
 for (var book : String in bookLines)
 {
     bookArray.Add(book);
 }
 

}

function OnGUI() { / Button to select book / if (firstTime) { if (GUI.Button(guiSelectBookRect,"Select Book")) { showBookList = true; } }else { if (GUI.RepeatButton(guiSelectBookRect,selectedBook) && firstTime == false) { if (showBookList) { showBookList = false; }else{ showBookList = true; } } }

 /*
     Shows list of books in button form
 */
 
 if (showBookList == true)
 {
     var top : int = 50;
     for (var eachBook : String in bookArray)
     {
         if (GUI.Button(Rect(0,top,400,20),eachBook as String))
         {

             selectedBook = eachBook;
             showBookList = false;
             firstTime = false;
             GetChapters();
             showChapterList = true;
             
         }
         top += 22;
     }
 }
 
 /*
     After book is selected and GetChapters() function is called
     
     Loads chapters in book in button form
 */
 
 if (showChapterList == true)
 {
     
     var top2 : int = 50;
     for (var chapter : String in chapterList)
     {
         if (GUI.Button(Rect(0,top2,400,20),chapter))
         {
             //print(chapter);
             var selectedChapter = chapter;
             showChapterList = false;
         }
         top2 += 22;
     }
 }
     

}

function GetChapters() { print(selectedBook); // var selectedBook : String = "Amos"; //if I uncomment out this line of code it works using the Amos book.

 var chapterTextArray : TextAsset = Resources.Load(selectedBook) as TextAsset;
     print(chapterTextArray);

 var chapterLines = chapterTextArray.text.Split(newLine);
 print("done");
 for (chapterLine in chapterLines)
 {

     if (chapterLine.StartsWith("CHAPTER"))
     {
         chapterList.Add(chapterLine);
         print(chapterLine);
     }
 }
 

}


I'm not sure if that code button worked so the above line marks the end of the code.

in the GetChapters() function at the end is where the problem is. If I uncomment out the line that assigns selectedBook the value "Amos", then the code works and shows all chapters of the book "Amos". But it doesn't work using a value set in the from the buttons.

Here is a copy of my Resources folder:

Resources.zip

and a copy of the script file: (note: the variable bookList is a textasset and needs the Books.txt file assigned to it in the inspector)

GUISelectVerse.js

I would appreciate any help.

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

TextAsset broken halfway 2 Answers

How to Deserialize an XML textasset? 2 Answers

Write physically files from resource folder into harddrive 2 Answers

Text file read, random quote C# 1 Answer

Assign public variables in design time... through code! 0 Answers


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