Search Unity

inconsistent line endings

Discussion in 'Scripting' started by hjbaard, Feb 6, 2010.

  1. JackofTraes

    JackofTraes

    Joined:
    May 28, 2014
    Posts:
    10
    I wasn't aware of how new it was, but I posted to let others know that there is an easier way to fix the problem. I was struggling to find any forum or stack-exchange posts that mentioned it. Sorry if it came off like I was criticizing the last post; that was not my intention.
     
  2. sristyh

    sristyh

    Joined:
    Jul 4, 2018
    Posts:
    1
    go to file advanced option from where??
     
  3. Khonkaengame

    Khonkaengame

    Joined:
    Aug 17, 2018
    Posts:
    4
    I want to share some magic just happened to me.
    On MacOS i use cmd + A copy all of code in the file from Visual Studio 2017 and paste in the to file on * Visual Studio CODE * then remove the last line (may not have to do this) and copy code back to Visual Studio 2017. The problem is just gone.

    I am trying to prove it one more time. but it just gone and not comeback again. Hope this help.
     
  4. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,171
    Ha ha! Thank you so much!!!!! :D :dancing:
     
    etzl and KenNoKage like this.
  5. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    129
    Yes, I logged in just to give a like. I finally get rid of these annoying warnings!
     
  6. azizulislam1

    azizulislam1

    Joined:
    Jan 28, 2019
    Posts:
    1
    Thanks. You have saved my day.
     
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    912
    I never understood why Unity bothers with this warning. Does it affect your scripts? Nope. Is it of any consequence? Nope. Then why on earth are we being bothered? Because. Can Vusiual Studio work with both? Yes. Can MonoDevelop? Yes. Is it of any consequence? Nope. Yet here we are being warned, but when I change a class Name so that it no longer matches the file name (with sometimes dire consequences) -- crickets.

    I really hope they remove this annoying, silly warning. It's in Visual Studio 2017 and MonoDevelop that came with 2017.

    Alternatively, can anyone give a plausible, COMMON use case that justifies the warning?

    -ch
     
  8. jonavan01

    jonavan01

    Joined:
    Feb 3, 2019
    Posts:
    2
    FIXED on Ubuntu in MonoDevelop. I've just did this: Ctrl+A, Ctrl+C, Ctrl+V. It seems that copy-pasting inserts new lines the correct way.
     
  9. FiredLight

    FiredLight

    Joined:
    May 11, 2019
    Posts:
    1
    What I do that works without having to change the script template or use any extensions is to use the Format Document tool in visual studio as soon as I open a new script. This has the added benefit of also changing the ugly default formatting (spaces between method brackets yuk). As long as you've set up your Code Style rules to your liking in Visual Studio's preferences this is the nicest solution imo.
     
  10. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    60
    I've had this issue for some time and tried all the fixes like changing the settings, but I still got the error.

    Then I got to this thread and read a comment on the first page that mentioned what line endings actually were, them being lines such as //r // r, so that's when it clicked in my brain that my variations of notes might cause the issue.

    If you thought (like me) that line endings were something at the very end of your script then I guess you're wrong. It also matters how you end your lines throughout your script. It still doesn't make sense to me how that's different per OS and why the Unity console kept buggering me about such a thing, but so far I found sticking to a consistent 'no space, or one space' in my script comments to be my solution.
     
  11. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    3,919
    Line endings have nothing to do with how you manually end your lines. Line endings refer to the invisible character code used to signify a new line. They appear at the end of every single line in a text file. You never see the line endings in your code. They are hidden, but are embedded in the text file (.cs file). (Some text editors allow you to see the line ending code if you enabled it.) Developers of the different operating systems made different decisions as to which character codes to use to end a line in a text file way back in the early days of the operating systems. If you open a text file on a Unix-based machine (Mac), it will encode new lines with one character, then you modify that file on a Windows-based machine, it may use Windows-style new line codes when you insert new lines. That's what the "inconsistent line endings" warning is telling you. (Generally, text editors will detect the new line style being used and won't mix new line code types regardless of the operating system in use. You can also choose a specific line ending style in most editors.)

    More details here;
    https://en.wikipedia.org/wiki/Newline
     
  12. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    60
    Thanks. Now I'm even more confused as to why I get that error then. I'm using Unity and Visual studio on a Windows 10 PC, alone, and don't share work (besides manually 'copying' code from tutorials and such) I have Unity set to 'native / Windows' line endings and I also got a similar setting enabled in Visual Studio. I'll just ignore that error from now on to keep my sanity.
     
  13. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    3,919
    Copying and pasting code in that uses different line endings could definitely do it. I don't think Visual Studio will convert the line endings for you when pasting from a file that uses different line endings. Many other things can lead to this such as when Unity changes code for you in your scripts (API updater). I've especially seen it happen with shaders when they silently change some deprecated API calls and add notes to the top of the file without using the same line endings as the rest of the file. Anything that may modify your text file is a potential culprit.

    VS has an option in File -> Advanced Save Options where you can set line ending style, but it sounds like you already have this set. You can also use common text editors to change the line endings in a file manually. For example, in Notepad++, go to Edit -> EOL conversion. Here's a thread on changing line endings in bulk across many files.

    Another common source of this issue which was mentioned in this thread is the Unity new C# script file template which defaults to Unix-style line endings. You make a new C# file in Unity (the default "public ClassName : MonoBehaviour", blah file), open the file in Visual Studio, make some changes, save, and bam, you get the warning. You added Windows-style line endings to a file that had Unix-style line endings to begin with. The setting in VS -> File -> Advanced Save Options -> Line Endings -> Windows (CR LF) should prevent this, but by default VS won't modify existing line endings by itself. One option is to modify the files in Program Files\Unity\Editor\Data\Resources\ScriptTemplates and change their line ending styles in a text editor. Unity also added an option to set the line endings for new scripts. See this post.

    Edit: It seems the VS -> File -> Advanced Save Options -> Line Endings -> Windows (CR LF) setting is a one-time setting that only applies to the file currently open for the next save. It's essentially the same thing as using Notepad++ to convert the line endings. It doesn't seem to enforce a specific line ending style throughout the solution.
     
    Last edited: Jul 21, 2019
    DonLoquacious likes this.
  14. Jeff-Goin

    Jeff-Goin

    Joined:
    Jan 29, 2015
    Posts:
    10
    This problem can also happen when you copy a code snippet and insert into your own file. This happened to me when copying from Unity's manual.

    A REALLY quick solution was to copy all the script code, import it into a simple editor that doesn't do formatting (like notepad), copy all the code from that, then replace what was in your script file with this now-stripped code.