# How to get the next line character to work in a self-typing dialog box?

With the help of a tutorial, I've created an almost successful dialog box that types itself out (just as older RPGs do, and some still do). The only problem is that I've wrapped the text so that if a word is too long for the line within the dialog box, it goes on down to the next line.

However, since my code is typing out the dialog character by character, it probably can't use the "\n" directly. And even when I do use "\n" it does not go to the next line. It simply prints those characters exactly.

I initially tried using a single character ("*") that my code would then replace during the typing with a "/n" character. However, this just printed the "/n" per usual. Whether I use '\n', "\n", or \n, it doesn't seem to work.

You can see, I've got some special characters being used to signal whether the text should go slower than usual, or whether or not there should be a longer pause. I also included the function that goes through and takes out those special characters. But anyway, here's the code in general, how can I add a next line?

private IEnumerator TextScroll(string lineOfText)
{
int letter = 0;
theText.text = "";
isTyping = true;
cancelTyping = false;
while (isTyping && !cancelTyping && (letter < lineOfText.Length)) {

if (lineOfText[letter] == '@')
{
letter += 1;
yield return new WaitForSeconds(0.5f);
}

if (lineOfText[letter] == '<')
{
typeSpeed = 0.5f;
letter += 1;
}

if (lineOfText[letter] == '>')
{
typeSpeed = 0.025f;
letter += 1;
}

theText.text += lineOfText[letter];
letter += 1;
yield return new WaitForSeconds(typeSpeed);
}

newLineOfText = lineOfText;
theText.text = TrimString(newLineOfText);
isTyping = false;
cancelTyping = false;
}

public string TrimString(string line)
{
var charsToRemove = new string[] { "@", "<", ">"};
foreach (var c in charsToRemove)
{
line = line.Replace(c, string.Empty);
}
return line;
}


Use Enviroment.NewLine instead of \n. On windows, this will return the value of \n\r, while on unix systems, this will return \n.

Test script I used to test this is below. Note, this uses functionality that requires 4.6 (the string interpolation).

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class testscript : MonoBehaviour {

Queue<string> toWrite;

Text text;

// Use this for initialization
void Start () {
text = GetComponent<Text>();
toWrite = new Queue<string>();
toWrite.Enqueue("hello" + Environment.NewLine);
toWrite.Enqueue($"Nice to meet you. {Environment.NewLine} how are you?"); StartCoroutine(write()); } // Update is called once per frame void Update () { } IEnumerator write() { while(toWrite.Count> 0) { var current = toWrite.Dequeue(); int idx = 0; while (idx < current.Length) { text.text += current[idx]; idx++; yield return new WaitForSeconds(0.1f); } } } }  Incidentally, in case you didn't know, idx+= 1; and idx++; are equivalent. • Hm... Do you know if it works on Mac--that's what I'm using. I tried to put it into the box, and it just prints the string {Environment.NewLine} itself. Aug 9, 2018 at 15:22 • I should probably have kept to .net 3.5 functionality. The$"Nice to meet you. {Environment.NewLine} how are you?" is what is called string interpolation, and needs .net 4.6. This is just a fancy way of writing "Nice to meet you. "+ Environment.NewLine + " how are you?". Environment.NewLine needs to be used in code, and should work on a mac. Aug 9, 2018 at 15:42