• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kinklade · May 09, 2015 at 09:18 AM · issue

New line in string issue

So I have an issue, where I need to use a string that contains a \n, but that does not translate into a newline in unity 5 for some reason.

So below is where I want to set this in the inspector:

alt text

Then I write this to the Canvas in the below function:

     public void addLogMessage(string Messagetoadd) 
     {
         logMessages.Add(Messagetoadd);
         
         LogPanelText.text = Messagetoadd;
         
         LogPanelText.text += "\n\nPress L to show / hide last log message.";
         
         showLogMessage();
     }

But that seems to not translate into a new line:

alt text

Can anyone help, what am I doing wrong here?

newline-issue-1.png (9.9 kB)
newlineissue-2.png (9.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by $$anonymous$$ · May 09, 2015 at 09:26 AM

I did some research on Google and this is what I found:

LINK 1

LINK 2

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kinklade · May 09, 2015 at 10:02 AM 0
Share

I have tried that and unfortunately that does not work either. In unity 4.6 you would see in the inspector that there was a carriage return, but now it just seems to add a space.

avatar image $$anonymous$$ · May 09, 2015 at 10:29 AM 1
Share

Well then, I've designed a special method, which will manage this new line character like so:

 private string $$anonymous$$anageText(string InText)
     {
         int CIndex = InText.IndexOf("\\n"); // Gets the Index of yur "\n" Char
         InText = InText.Remove(CIndex, 2); // Removes "\n from original String"
         InText = InText.Insert(CIndex, "\n"); // Adds the actual New Line symbol
         return InText;
     }

Try to play with Input String and you will see the results :)

avatar image Mehrdad995 $$anonymous$$ · Dec 26, 2017 at 01:52 PM 0
Share

it's like turning the food two times around your head and then put it in your mouth. why not just using InText.Replace("\\n","\n"); return InText; ???

avatar image kinklade · May 09, 2015 at 11:10 AM 0
Share

Perfect! Worked like a charm :)

Thank you very much

avatar image
1

Answer by kinklade · May 09, 2015 at 03:22 PM

So for anyone else that might need to do this, it was achieved with the following funtion:

 private string newLineReplace(string InText)
     {
         bool newLinesRemaining = true;
         
         while(newLinesRemaining)
         {
             int CIndex = InText.IndexOf("\\n"); // Gets the Index of "\n" 
             
             
             if(CIndex == -1)
             {
                 newLinesRemaining = false;
             }
             else
             {
                 InText = InText.Remove(CIndex, 2); // Removes "\n from original String"
                 InText = InText.Insert(CIndex, "\n"); // Adds the actual New Line symbol
             }
         }
         
         return InText;
     }

you can then say something like this to show it

 LogPanelText.text = newLineReplace(Messagetoadd);

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mehrdad995 · Dec 26, 2017 at 01:54 PM 0
Share

it's like turning the food two times around your head and then put it in your mouth. why not just using

InText.Replace("\\n","\n"); return InText;

???

avatar image
0

Answer by jrramirez3 · May 30, 2019 at 08:42 AM

Hello!

New Line: "\r\n" instead of "\n" worked for me. I got this from this one: https://answers.unity.com/questions/16865/making-newline-n-in-inspector.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Realtime Lighting Indirect issue 0 Answers

input axis gas pedal is not steup unity 0 Answers

The ‘ContextClick’ event of the ‘TreeView’ class does not fire from a custom inspector; but if you do it from an editor window it fires. 0 Answers

Cant find issue tracker's resource 0 Answers

I can't open my Unity Project 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges