• 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 Alayna · Jan 08, 2013 at 04:04 PM · arraytextvariablereadstreamreader

Getting A Variable From a Outside Text File

Hello, I have been trying to grasp how you would go about reading variables from a text file that is for example in the root folder of the Unity Project. I would like to use t$$anonymous$$s to change certain aspects of the game without having to rebuild it, for example change the color of a cube after the standalone containing it is already built.

Currently I have found t$$anonymous$$s script from the forums, and it works perfectly at what it was intended to do, display the text lines in print on the console.

  import System.IO;
     
     var textName : String;
      
     function Start () {
         try {
             // Create an instance of StreamReader to read from a file.
             sr = new StreamReader(textName);
             // Read and display lines from the file until the end of the file is reached.
             line = sr.ReadLine();
             w$$anonymous$$le (line != null) {
                 print(line);
                 line = sr.ReadLine();
             }
             sr.Close();
         }
         catch (e) {
             // Let the user know what went wrong.
             print("The file could not be read:");
             print(e.Message);
         }
     }

I have a text file in the root of my project with lines that go like

 "
 cube1 isGreen
 cube2 isRed
 cube3 isYellow
 "

I also have a managerial script in the game that interprets the strings and causes them to change the colors of the objects in question. However I cant figure out how to take these lines, w$$anonymous$$ch are currently only printed, and put the information into a list that I can use to give to the manager script.

Ideally I would like for the "cube#" part to be left out, and only read the line number and the variable ie the first line in the array would say "isGreen". Either that or It could have "var cubeOne : String;" and place the "isGreen" there, if an array would be too difficult.

If anybody would be willing to explain t$$anonymous$$s to me I would be extremely appreciative!

Comment
Add comment · Show 4
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
avatar image Next Beat Games · Jan 08, 2013 at 10:28 PM 0
Share
avatar image Next Beat Games · Jan 08, 2013 at 10:29 PM 0
Share
avatar image Alayna · Jan 09, 2013 at 04:40 AM 0
Share
avatar image Alayna · Jan 09, 2013 at 06:37 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Itinerant · Jan 08, 2013 at 05:07 PM

My first thought would be to switch your text file into a more ordered system, like a CSV spreadsheet. Perk to t$$anonymous$$s is that it'll be easier to modify using excel, instead of dealing with a text file. Here's a script I'm using to do just that, creating a dictionary with the first CSV column as the key, and everyt$$anonymous$$ng else stacked be$$anonymous$$nd it. Right now I'm using t$$anonymous$$s to attach about 20 attributes to gameobjects, so it works well :) It skips duplicate keys and lines with an empty first column.

 //Load a dictionary with spreadsheet data, taking a text file and a dictionary as arguments.
 function LoadCSVfromTXT(csvAsTxt : TextAsset, dictionary : Dictionary.<String, List.<String> > ){
 
     var tempArray : String[];
     var tempArray2 : String[];
     
     //Split text file by lines
     tempArray = csvAsTxt.text.Split("\n"[0]);
     
     //For as many lines as exist, split by commas. If the first value isn't already in the dictionary,
     //and isn't n/a, add it to the array.
     for(var x=0; x<tempArray.Length; x++){
     
         tempArray2 = tempArray[x].Split(","[0]);
         
         if(tempArray2[0] in dictionary || tempArray2[0]=="") continue;
         else{
             dictionary[tempArray2[0]] = new List.<String>();
             for(var y=1; y<tempArray2.Length; y++){
                 dictionary[tempArray2[0]].Add(tempArray2[y]);
             }
         }
     }
 }
Comment
Add comment · Show 18 · 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
avatar image Itinerant · Jan 08, 2013 at 05:08 PM 0
Share
avatar image Alayna · Jan 08, 2013 at 05:37 PM 0
Share
avatar image Itinerant · Jan 08, 2013 at 05:43 PM 0
Share
avatar image Alayna · Jan 08, 2013 at 07:28 PM 0
Share
avatar image Itinerant · Jan 08, 2013 at 09:18 PM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Read variable from txt to Vector3 1 Answer

How can I read data from a text file, putting a large amount of data into structures 2 Answers

How to put Streamwriter into an array? 1 Answer

Variable Vs Array? 2 Answers

Save what you write in textarea as variable? 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