Search Unity

bug with the import of a line .txt with a TextAsset

Discussion in 'Scripting' started by toadygamer, Nov 16, 2019.

  1. toadygamer

    toadygamer

    Joined:
    Dec 20, 2018
    Posts:
    5
    Hi,
    when I import text from a .txt and try to compare the text of it and the text of an InputField, it does not work. Except that I managed to find the problem is that if I run my game and put the same thing in InputField and in my Text, I have to remove a space in the text import for it to work. It's as if there was an invisible space or something else.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Script : MonoBehaviour
    7. {
    8.     public TextAsset textFile;
    9.     public Text displayWord;
    10.     public InputField inputWord;
    11.     public int line;
    12.     public string[] textLines;
    13.  
    14.     void Start()
    15.     {
    16.         if (textFile != null)
    17.         {
    18.             textLines = (textFile.text.Split('\n'));    //To have the line of the .txt
    19.         }
    20.  
    21.         displayWord.text = textLines[line];              //To take the work of .txt and writte this in "displayWord"
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.                                                         //If the word displayed is the same written in "inputWord"
    27.         if (inputWord.text == displayWord.text)
    28.         {
    29.             Debug.Log("It's work ! 1");
    30.             Next();
    31.         }
    32.     }
    33.  
    34.     public void Next()
    35.     {
    36.         line++;
    37.         inputWord.text = "";
    38.         displayWord.text = textLines[line];
    39.     }
    40. }
    41.  
    upload_2019-11-16_18-18-24.png

    Where is the arrow, i need to wipe off. And when i do that, it's work :/
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Different operating systems use different delimiters to signal new lines.
    So, you can't just say /n and decide it's going to work everywhere, and with every library that is designed to work with different operating systems.

    Windows uses "\r\n", afaik. 2 characters. Others can use \n, or \r
    Maybe TextAsset is using the windows delimiters, because you're using windows?

    This is a very common problem for developers, who are processing data from different operating systems, so you should search online for solutions, there will be a lot of threads on this.

    Off the top of my head, I don't remember the best work arounds to account for these different delimiters. There are ugly and efficient ways of doing it, and there are easy ways of doing it.

    If you have large files and you can't read and process everything at once, you'll have to go for ugly and efficient, which really means reading one character at a time and catching these delimiters.

    Others working with smaller data might replace characters in the data, because they are able to process all of the data in a file at once. Like they'll replace \r\n with \n, and then they'll replace \r with \n. Now all new lines are signalled by \n and they can split it with one character...

    Check what other people are doing though, and decide which work around is best for your situation.
     
    Last edited: Nov 16, 2019
  3. toadygamer

    toadygamer

    Joined:
    Dec 20, 2018
    Posts:
    5
    Thx ! I will do that. thx for your help and have a good day.