- Home /
Cannot get newline to show in guitext from script
I am trying to insert a newline into a guitext from a script, but it doesn't show up.
I have tried adding \n
, System.Enviroment.NewLine
and a string containing a literal newline but none show in the guitext.
The strange thing is when I output the contents of the guitext to a log file and open it, the newlines are there.
Anyone have any idea what the problem could be?
EDIT:
The demo can be found here.
And here's the code I'm using, with the end goal of text wrapping the text to the size of the background, and then outputting it character by character. Also, just isolated the problem to the second part that outputs the text character by character.
IEnumerator TypeWriter()
{
string[] words = currentNode.text.Split(' ');
for (int w = 0; w < words.Length; w++)
{
if (w > 0)
guitext.text += " ";
guitext.text += words[w];
if (guitext.GetScreenRect().width >= guitexture.GetScreenRect().width * .8f)
{
guitext.text.Remove(guitext.text.Length - words[w].Length + 1);
guitext.text += newline + words[w];
}
}
guitext.text = "";
for (int i = 0; i < currentNode.text.Length; i++)
{
guitext.text += currentNode.text[i];
yield return new WaitForSeconds(typewriterInterval);
}
}
Answer by HappyMoo · Jan 01, 2014 at 03:53 PM
If you set a variable from code, it's \n, if you set it in the inspector, you can try a [Multiline] Attribute, to get a multiline text field.
Are you sure your GUIText is big enough to show both lines?
Thanks for the reply. I have tried adding the [$$anonymous$$ultiline]
attribute but still get the same result.
Ok... let me get you through a simple test:
In the hierarchy: Create Guitext
Add new Script Component:
using UnityEngine; using System.Collections;
public class GUITest : $$anonymous$$onoBehaviour { void Start () { guiText.text = "1\n2"; } }
Start the game and you see the guitext having two lines.
Ok, so the test works. Still can't get it to show using the same guitext as the test with my original script, have edited to put the code and a link to a demo. Thanks for the help so far :)
Hello,
I do not have a good solution, but I tried the same thing two days ago and had the same issue. I do not understand why, but when adding "\n" in a "for" or "foreach" function, it does not work. Though adding it manually (as Happy$$anonymous$$oo proposed) does work. $$anonymous$$y (horrible) solution was thus to add all manually like this:
guitext.text[1] + "\n" + guitext.text[2] + "\n" + guitext.text[3] + "\n" + guitext.text[4] + "\n" + ...
This might give you a hint to solve your problem
@Gr$$anonymous$$l Thanks, but as this code will be used to show text of any length it would be impractical to do it as in your example.