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 AngryCSharpNerd · Oct 17, 2012 at 03:15 AM · randomfiletextasset

Text file read, random quote C#

 //YES, there is the monobehavior part and all that, but i'm not concerned with it right now.
 
 string quote;
 public TextAsset splashFile;
 void Start(){
     string[] dataLines = splashFile.text.Split('\n');
     string[][] dataPairs = new string[dataLines.Length][];
     int lineNum = 0;
     foreach (string line in dataLines){
         dataPairs[lineNum++] = line.Split(',');
     }
     quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
 }

/*I have absolutely no idea what I am doing or what I am reading exactly. I have a text file that works like this:

hello,hi hey,hru

(For some reason the enter button isn't seperating between "hi" and "hey,") I am trying to get this script to work by having it select a random quote from the text file. What is this lineNum++, and what code did I just type in? I tried to manipulate it from this answers post but no luck so far: http://answers.unity3d.com/questions/16225/how-can-i-read-data-from-a-text-file-putting-a-lar.html

The error I get from my fail code is

"IndexOutOfRangeException: Array index is out of range. _StartMenu.Start () (at Assets/Resources/Scripts/_StartMenu.cs:41)"

Please help me get this code, and please try to explain nearly every line from void Start(){ to the end of the foreach part, because I am totally confused on what it's trying to do. */

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 Muuskii · Oct 17, 2012 at 05:15 AM

 string quote;
 public TextAsset splashFile;
 void Start(){
     string[] dataLines = splashFile.text.Split('\n');
     string[][] dataPairs = new string[dataLines.Length][];
     int lineNum = 0;
     foreach (string line in dataLines){
        dataPairs[lineNum++] = line.Split(',');
     }
     quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
 }

First two lines, declare a String and a TextAsset. You don't mention instantiating the text asset and that answer that you linked to only vaguely mentions that you must drag the text asset (the .txt file) into the slot where it says "splashFile" in the inspector while viewing a GameObject that has your script attached.

It is at this point that I need you to confirm that you have done this because it is a common mistake to make and can sometimes lead to similar errors, but probably not this one.

You indicated that you do not understand what "lineNum++" means: I would suggest that if you do not have any, or much programming experience that you first attempt to learn a language in a more formal (and supported) environment as there are more tutorials for a coder of your level for good 'ole C# than C# in Unity, at your level.

Continuing on for an explanation of the remaining lines:

 string[] dataLines = splashFile.text.Split('\n');
 string[][] dataPairs = new string[dataLines.Length][];

The first line (presumably) takes ALLLL of the text from the file and loads it into a string, and then splits it at every instance of the '/n' character. Which is usually the newline. (UnityAns is parsing out the slash, so just pretend that it's the right direction)

 int lineNum = 0;
 foreach (string line in dataLines){
    dataPairs[lineNum++] = line.Split(',');    //This line here
 }

"This line here" is splitting each line at the comma.

This means that "Hello,/n"

After those two splits will become: ("Hello" , null )

Note how there is nothing after the comma (the newline is taken off from the first line) and since the elements of the array were previously undefined (null) then the later line.

 quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];

May actually throw a nullreference exception. However, since the second dimension of the array is also not set before being accessed by this line it doesn't have a chance to throw said exception but instead will complain about the array not even having an element to access there.

(It may be confusing how that is different from a nullreference exception, but believe me. . .there is a difference)

Well, that was a wall of text. I hope it's at least helpful. My suggestion for this code is that it needs some very serious refinement and some refactoring to be more in line with your needs, which need to be more defined for me to fully write a derived version.

Thank you very much for your time.

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

10 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

Related Questions

Resources.Load not giving me all data for a .txt file 1 Answer

How does a TextAsset work? 1 Answer

Reading Data generated in Processing from a TextAsset 0 Answers

How to open/read/write CSV file once the app is built? (like Resources.Load but whit the built app at Application.dataPath) 2 Answers

Basic Question about TextAssets 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