Using System.Environment.NewLine Not Working on Android

I created a simple “typing” script. It’s working on PC just fine… but it’s not doing the “line break” on Android. Any ideas? Here’s the code:

	void Update () {
       
        if ((Time.realtimeSinceStartup - _lastUpdate) > updateSpeed)
        {
            if (_cnt + 2 <= credits.Length)
            {
                if (credits.Substring(_cnt, 2) == System.Environment.NewLine)
                {
                    //this isn't running on Android, but works on PC
                    //add a line break (move position down by lineBreakSize
                    gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + lineBreakSize, gameObject.transform.position.z);
                }

                _lastUpdate = Time.realtimeSinceStartup;
                gameObject.GetComponent<UILabel>().text = credits.Substring(0, _cnt);
                _cnt++;
            }
        }

	}

I’ve also tried:

if (credits.Substring(_cnt, 2) == System.Environment.NewLine | credits.Substring(_cnt, 2) == "

" | credits.Substring(_cnt, 4) == "
")

Just to add more information credits is just simply defined as a string:

public string credits;

I typed up the text in notepad (just a txt file) and paste the text into the Unity Editor into that string variable.

See System.Environment.Newline documentation – newline is only one character on unix systems ("
", not "\r
" – note that the slash indicates an escape sequence, it’s not a character in the string), so comparing a length 2 substring was never going to work. Splitting up your string into an array of strings sounds like a much cleaner approach.

I’ve had the same problem and found a sloppy solution for detecting line breaks / carriage returns in C#. Maybe you or someone else might found it useful.

I had a text file containing words, each word written down on a new line. I used

allWordsText.Split(System.Environment.NewLine)

to make an array of words out of this. On PC this worked just fine but on an Android device it didn’t work. Solved it by using multiple carriage return characters:

string cReturns = System.Environment.NewLine + "

" + “\r”;
string words = allWordsText.Split(cReturns.ToCharArray());

The string.Split() function can handle multiple seperation characters (as cleverly suggested here). This worked on my Android device, neatly splitting the words and putting them in the array. Hope someone might find this useful or has a better suggestion :slight_smile:

I’ve decided since there were not any suggestions on here that I’m going to just put each text line into a string array. That way I will know when that string array element is at the end and I can move the gameObject up.