Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Inconsistent Line Spacing

Discussion in 'Getting Started' started by ChimboSlice, Feb 1, 2017.

  1. ChimboSlice

    ChimboSlice

    Joined:
    Feb 1, 2017
    Posts:
    2
    Hello I am brand new to the unity engine. I was going through the beginner's tutorial, the one with the ball. After creating the script that makes the ball move around the plane I got this warning "There are inconsistent line endings in the 'Assets/Scripts/PlayerController.cs' script. Some are Mac OS X (UNIX) and some are Windows.
    This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands.
    " Does anyone know how I can use the Convert Line Endings commands? If so where would I find these in order to use them?

    Thanks in advance
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    6,771
    That depends. What platform are you running on? What text editor(s) do you have on your system?

    (And FYI, since we're on the subject of text editing, I do all of my Unity coding with Script Inspector 3, which is so much dramatically better than MonoDevelop that I couldn't bear to use the latter anymore. Totally worth every penny. Please check it out!)
     
    Flipbookee likes this.
  3. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    ChimboSlice,

    As Joe stated, it depends on the editor and platform you are using. My guess, if you are like me, you are using the Microsoft Visual Studio community version that downloads when you start using Unity, and you are on a Windows computer.

    There is a free course on Unity over at DevU.com, and the instructor says that is because the version on Monodevelop it is using was originally set up for Mac. He shows how to change the settings to get rid of those errors, but it is pretty complicated so I just use it as it came. Whenever I get those errors I just click convert in the pop up window and it fixes it and I've never had any issues.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    2,682
    Just a bit of background on what the error actually is:

    It's not line spacing you're having an issue with, it's line endings. In Windows, line endings are typically a carriage return ( \r character ) along with a line feed ( \n character ). Unix systems, including Linux and OSX/macOS, use just the line feed ( \n character). Bear in mind, these are invisible characters that you don't normally see when editing a text file in an editor.

    If you open a file that was generated using Unix line endings on your Windows computer and add a few lines, it's likely you now have a mix of the two types of line endings. The worst I can imagine this does is cause inconsistency when opening the file on a different operating system, but Unity/Mono are being nice here by letting you know about it anyway. And as the message indicates, and as Ryeath mentioned, you can easily resolve this by opening the file in MonoDevelop and using the Convert button in the prompt indicating the mixed endings.
     
    jhocking, Mungiu and Ryeath like this.
  5. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Thanks for the explanation Schneider21.
     
  6. ChimboSlice

    ChimboSlice

    Joined:
    Feb 1, 2017
    Posts:
    2
    Ryeath,
    Yeah I am using the Microsoft Visual Studio that came with the Unity download, and thank you for pointing me towards DevU.com
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,049
    You can also safely ignore the warning. Its not one that actually makes a difference if you fix it.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    11,537
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,723
    It does make a difference, since any errors/warnings will not likely point to the correct line number. So you should fix the problem.

    --Eric
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,049
    Has never happened to me. And I've been to lazy to do anything about the inconsistient line endings for years.

    There is no reason not to fix it. But there is no compelling reason to fix it either.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,723
    Since it's so trivially easy to do, does in fact prevent cases where errors point to wrong line numbers, and stops the warning from annoying you, yes there's a reasonably compelling reason to do it. :p

    --Eric
     
    Mungiu likes this.
  12. Nighfox

    Nighfox

    Joined:
    Jan 9, 2018
    Posts:
    4
    To add in Schenider21's and Eric5h5's answers:

    Line endings
    CR+LF (Carriage return and Line Feed [ /r/n ]) are for Windows
    LF (Line Feed [ \n ]) are for Linux and newer Mac / OSX versions (version 10 and above)
    CR (Carriage Return [ \r ]) are for old Macs (version 9 and earlier).

    The Problem
    Usually as what they've mentioned, what happened here is that you have intermixed either one or more of the line ending types specified above. You can noticeably see this when you create a new C# script from the assets editor menu. The default script template is currently in LF, so if you're using Windows (CR+LF), and overwrite the script, you now have a mixture of LF and CR+LF line endings, leading to said warnings.

    You can also experience this if you've copy-pasted a code snippet from somewhere in the web, which could contain different line endings. If you have a script with CR+LF line endings, and you pasted a snippet containing CR endings, you now have a mixed line ending file with CR+LF and CR endings.

    Why even bother?
    At first glance it just seems pointless but as Eric5h5 have mentioned, this can lead to mysterious problems later on. The only thing some people who have not bothered to deal with it think that it doesn't make any difference is that they haven't been in one situation where line consistency is tantamount. Even so, when left alone without any action for too long, it can be extremely frustrating to get those warnings in Unity and receive a popup in Visual Studio every damn time you open the script with mixed endings.

    Another notable problem you can get from this is if you're using version control, which (depending on the system) will either complain or perform unwanted behaviors if your files contain mixed line endings.

    Other Solutions
    You can just solve this of course by selecting the line ending you want using your code editor (a popup will appear if you're using VS) and selecting to convert, but if you have a lot of scripts in your project, that could get annoying and tiresome pretty quickly. One good suggestion Ryiah had mentioned was to edit the script template and replace it with your own script with your desired line ending.


    My Solution
    I really hate shameless advertising or anything, but I figured I might as well share a lightweight editor tool I made called Consistent Line Endings, since it's...well, for free :p It basically scans for line endings in your files (it supports a wide variety, including even shaders, compute files, etc), and normalizes and converts them for you. You can choose one of the three line endings--CR, LF, or CR+LF. If your file contains a mixture of them, everything will be fixed.

    Using it, you can start fixing line endings either manually (right-click in project view, then choose normalize all files) or automatically (every time you make a change in your scripts, it automatically converts all of it for you). So basically, it's your choice. Plus, it even outputs a log containing info about the conversion process, so you could check what line ending types do your files have previously if you want. You can also filter certain extensions that you want to convert and those that you don't.

    So far, this tool helped me to get rid of those annoying warnings and popups, so I wanted to share it to anyone who's having the same problem. It's completely free so if you want to give it a try, go ahead and get it here. :D
     
    Ryiah likes this.
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,660
    I'm just going to add here, as a note for future users.

    I used to open the new monobehaviour template and edit it ('save as' with my desired line ending, so any new script I made had the proper endings. I had to do this everytime I updated Unity.

    However, now there is an option in the editor to handle this for you. Someone pointed this out in another thread, so I thought I'd share it, again.
    Under Edit -> Project Settings -> Editor : you'll see an option (for me, atm, it's at the bottom) "Line Endings for New Scripts". Simply choose there. :)
     
    Ryiah likes this.
  14. Nighfox

    Nighfox

    Joined:
    Jan 9, 2018
    Posts:
    4
    That's really convenient, but take note that it has its limitations:

    Firstly, it's feature only available starting from Unity 2017.3, so for those who are using the previous versions, they might have to resort to other solutions ;)

    Not only that, but that feature ONLY covers Unity default script templates, so say if you receive a script file from someone containing LF (Unix / Newer Macs) line endings, and you're using Windows (CR+LF), and you edit the script, that's still going to result into a line inconsistency warning (followed by an annoying popup if you're using VS) even though you have the option in the Editor Settings to Windows.

    That's because it doesn't necessarily convert / normalize your script files--it just merely gives you an option to choose what script template line ending type you want for newly created scripts, but not for existing and custom ones.
     
    Last edited: Mar 4, 2018
    Ryiah likes this.
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,660
    Of course :) Just wanted to add it out there..
     
  16. Nighfox

    Nighfox

    Joined:
    Jan 9, 2018
    Posts:
    4
    Anyway, it's convenient in its own way, so thanks for sharing it :) Might be a start for some people out there. Actually, I never knew about that feature up till now lol :p
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,723
    Just thought I would point out nobody would ever use \r...it's not just "older Macs", it's the ancient, pre-OS X Macs, which used a different operating system entirely and I expect even the last of the die-hards stopped using a decade ago. You can safely ignore them...Unity never ran on them anyway. ;) Better to remove that option to prevent anyone from accidentally using it.

    --Eric
     
    Ryiah and Nighfox like this.
  18. Nighfox

    Nighfox

    Joined:
    Jan 9, 2018
    Posts:
    4
    Good point :D I'm not sure if that's why shader files seem to be outputting errors and warnings when using that line ending tho :p
     
  19. banreaxe

    banreaxe

    Joined:
    Jan 5, 2012
    Posts:
    427
    i cant believe this is still an ongoing issue. its nice to see there is an option to fix it in the unity editor preferences, but why the heck is it not automatic? if im using unity on windows i would like it to default to windows line endings, that should be a no brainer. if your working on a project with others and the consensus is to use different line ending then fine change it then, but it should default to the os you are using.
     
    Flipbookee likes this.
  20. Flipbookee

    Flipbookee

    Joined:
    Jun 2, 2012
    Posts:
    2,224
    It is automatic, but only in Si3. I’ve never seen any other code editor doing that automatically and I have no idea why is that! But Si3 is made for Unity specifically and as such of course it will never bother the user asking to fix their line ending or to choose this or that line ending style as both work just fine. It simply just fixes it silently on save and let’s you do what you actually wanted to do - some programming. So Si3 users have even forgotten that warning still exists, they never see it :cool:
     
    JoeStrout likes this.