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 TargonStudios · Mar 09, 2015 at 10:59 PM · javascripttext

Problem with text from a .txt file.

So lately I have been trying to create a script to read .txt files and split it into chunks for NPCs and dialogue. Except, I have run into a problem, when I try to see if something from the text file is equal to a piece of text in the script, they never are, when even they should be. Are there hidden spaces or something when a .txt file is turned into a string? Here is an example of what I'm am trying to say.

 var textFile : TextAsset;
 var DialogLines : String[];
 
 function Start(){
 if(textFile){
 DialogLines = (textFile.text.Split("\n"[0]));
 }
 }
 
 function Update(){
 if(DialogLines[0] == "TheTextToFind"){
 print("Found it");
 }
 }

In this case, the first column in the text file would be "TheTextToFind", and yet it never works.It seems like it should be simple enough, but it doesn't work at all, and I can never locate anything/find anything. (I am sure that there is not mistake in the text file/word being used to search, I have tried searching for many things, coming up with nothing)

Any help is greatly appreciated!

Comment
Add comment · Show 6
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 Glurth · Mar 09, 2015 at 11:15 PM 0
Share

Split("\n"[0]);

Depending how how the text file is formatted, sometime a newline is TWO characters (line-feed & carriage-return). When you say [0], this will specify only one character. But it looks like Split(), can actually split based on a string (as opposed to a single char), so I suggest you simply remove the [0].

I would also suggest you use Debug.Log(DialogLine[0]); immediately after you call the split function, to confirm the text is indeed splitting as expected.

avatar image TargonStudios · Mar 09, 2015 at 11:33 PM 0
Share

I have spent lots of time trying to figure this out, and put many Debug.Logs. Nothing seems to be wrong.

When I try removing the [0], an error pops up: Assets/Common/WorkingAssets_Network/Controllers/test.js(12,35): BCE0023: No appropriate version of 'String.Split' for the argument list '(String)' was found.

Thanks for your reply!

avatar image FirePlantGames · Mar 10, 2015 at 03:55 AM 0
Share

I would suggest using a for loop ex;

 for (var i : String in DialogLines)
 {
      //code
 }

should work?

avatar image TargonStudios · Mar 10, 2015 at 09:22 AM 0
Share

$$anonymous$$y main script already has something that does that, but there is no point in posting that because if I can't even get something as simple as the above to work, I know my main script won't work. That is the only part that doesn't work, when it should.

avatar image Glurth · Mar 10, 2015 at 03:07 PM 0
Share

I don't know why everyone want you to loop through your string. Oh I see now, it's not the string they want you to loop though, they want you to loop through all the DialogLines (so as to check more than just the first dialogline). I'd like to take it one step at a time, before trying to find text in the string (for which I would use the string.Contains() function- as Todd suggested), lets try to figure out why the Split function doesn't work for you.

If it's not finding the split character then the ENTIRE string should end up in DialogLines[0]. What does you debug show for this array element? completely empty? What does size of the DialogLines array show in debug.log, after calling split?

going yet another step backwards, can you confirm that textfile.text variable actually has the text you want in there (also with debug log)? Perhaps you need to specifically load the asset before the textfile.text variable is filled with a string?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Mar 10, 2015 at 04:28 AM

memorymod is right. Where is the loop to search your array????

 for(var n = 0; n < DialogLines.Length; n++){
  
       if(DialogLines[n].Contains("TheTextToFind")                   {yay=DialogLines[n];}
  }}


Comment
Add comment · Show 1 · 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 MagicZelda · Mar 10, 2015 at 08:50 PM 0
Share

are u trying to get first column if so (not sure if [0] is in right place), not tried this but worth a look

DialogLines = (textFile.text.Split("\n"[0]));

try

DialogLines = (textFile.text.Split("\n")[0]);

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Quest Script Help 2 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

How to make GUI Text appear after a certain amount of time 2 Answers

Text wont change to correct value 1 Answer

Detect Text in GUI; Print 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