linebreaks in string variables

Hi all…

in my program I have loads of text that is stored in sepperate String variables.
More often than not I have to have linebreaks to display the whole text.
The manual says that one should use “/n” to break lines but so far I have not been successfull at that…

this is what I do:

var avatarDialog = "this sure is a long line /n thank god for the line break";

and this is not working

… can anyone help?

Eggert

It should be \n and not /n.

var avatarDialog = "this sure is a long line\nthank god for the line break";
4 Likes

tried both… doesnt work :frowning:

Eggert

Could you post the script you are using so we could take a look at what might be wrong?

Well, what are you doing with the string? Trying to log it? Display it through a GUI_Text object? Something else? The string likely isn’t the problem, it’s probably the receiver.

“doesn’t work” needs some serious details and clarification added to it.

sorry that I didnt clarify this well enough.

What I am trying to do is use GUI_Text object to show text on screen.

following is an example of a string that I store in a “masterDialog” file.

var mamba_in_id3 = "Say what!!! A liposuction of a 1000 pounds!!!\n You saved my ass, sister, \ngo ahead and take those sneakers,\n soon I'll be able to walk and get my own!";

And the following is the how I get it on the screen:

if (state == 1){
  dialogGui.guiText.text = dialog.amira_mamba_in_id1;
  }

and this is how I get my masterDialogScript, which is basically a script that holds a bunch of string variables with the my dialog:

var dialog: masterDialogScript;
dialog = obMaster.GetComponent(masterDialogScript);

everything works fine except the linebreaks.

I hope I have made myself a bit more clear this time.

and thanks for your help…

eggert

What does amira_mamba_in_id1 look like? Because you’re currently displaying that string, not mamba_in_id3.

ahh… well didn´t notice I posted 2 different string names… but that does not matter… I have tried to break many of the strings…

for the record this is the amira_mamba_in_id3

var amira_mamba_in_id1  = "Amira from down the block";

this is the whole conversation:

var mamba_in_id1        = "Who's there?";
var mamba_in_id1_audio: AudioClip; 
var amira_mamba_in_id1  = "Amira from down the block";
var amira_mamba_in_id2a = "1. Can I get those fat sneakers?";
var amira_mamba_in_id2b = "2. How do you get out of here?";
var amira_mamba_in_id2c = "3. What is your weight";
var mamba_in_id2a       = "My fat pair of sneakers,  the ones I won from the\nSSSBBBW\n(super-sized-sexy-big-black-beautiful-woman) \n contest. No way I'm saying, ya hear, no way! \n I might not be able to see them, \n but I know they're there and they're fly. \n Well.., if you can get me out of here I may consider";
var mamba_in_id2b       = "You tell me, your skinny chocolate bar, \nI haven't been out since the SSSBBBW contest. \nIt took three cranes. Can you help me?";
var mamba_in_id2c       = "At the SSSBBBW contest it was 1000 pounds.";
var mamba_in_id3        = "Say what!!! A liposuction of a 1000 pounds!!!\n You saved my ass, sister, \ngo ahead and take those sneakers,\n soon I'll be able to walk and get my own!";
var mamba_out_id4       = "I would like to order some food, for delivery please, 10 deep fried chickens and 6 bottles of coke. Thank you.";
var mamba_after_id5     = "Thank you sister";

as I said earlier, I can display the text but the \n doesnt seem to work …

eggert

You got me. It looks to me in my testing that GUIText doesn’t support new lines but this thread leads me to believe that it does:

http://forum.unity3d.com/viewtopic.php?t=1730&highlight=guitext+newline

So … I dunno.

This is a shot in the dark on my part, but are you using a text encoding other than UTF-8 for your scripts? Line breaks are working fine for me.

1 Like

It looks like you are thinking that editing the values in the script will update the ones in the attached component.

Please note that the string at the left of the assignment is only setting the default value for the variable. It will not change if the script has already been attached to a game object.

Either remove the MasterDialog script from you game object and reattach it or reset it in the context menu in the game object inspector.

It works now… thanks to the reset button in the inspector…
Thank you Freyr…

quite handy to know this :slight_smile:

Eggert

FYI, I just ran into this problem. But for me the newlines were getting encoded into visible text. This may be because I was setting the string values directly from the inspector.

In any case, the fix is just the following:

newText = newText.Replace(“\n”, “\n”);

15 Likes

This solved my issue, thanks Doug.

3 Likes

I had the same problem and this solved it.

I was, in fact, setting the string values through the Inspector window, and this fixed it for me.

Thank you very much!!! (:

1 Like

Amazing how this solution, from 14 years ago, is still up to date!

Worked for me, thank you!

3 Likes