Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Feedback Random using directives appear in my code when I build

Discussion in '2023.1 Beta' started by dlorre, Nov 27, 2022.

  1. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    It's not everytime but sometimes I get build errors and when I look into the code I see random using directives added to my code:
    upload_2022-11-27_18-39-57.png

    upload_2022-11-27_18-38-30.png



    I remove them then I can build normally. I can't tell when it started but I'm actually building with Unity 2023.1.0a19

    upload_2022-11-27_18-43-0.png
     
    Gravesend likes this.
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    You're not alone in observing this. I find it annoying but during the code check-in process I review the changes and notice things like that. They don't cause harm but like you asked "who is doing this and why?"
     
  3. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    396
    It is very odd and adds the most random lines sometimes! End up with about 30 usings that I didn't type, and sometimes causes build errors. It's to do with VS 2022. Haven't looked into turning it off though.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    It's probably Visual Studio adding those.

    Both VS and Rider will add using directives for things you auto-complete. If you auto-complete something you didn't intend to, and then backspace instead of undoing, the using directive will stay. It might be that VS also has a bug where undoing the auto-complete doesn't undo the automatic inclusion of the using.
     
    Gravesend likes this.
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    It could be VS but that is quite a list of mistaken using statements. Mine seemed pretty random but I found there is a setting in VS to prevent it so let's see if that works. I like being informed but I prefer to acknowledge the inserting of lines into my source.

    Tools/Options/Text Editor/C#/Advanced "Add missing using directives on paste"
     
    jlnorris, monark, MihkelT and 2 others like this.
  6. nalex66

    nalex66

    Joined:
    Aug 26, 2022
    Posts:
    54
    THANK YOU!! I recently switched from VS 2019 to VS 2022 while testing Unity 2022 LTS, and this has been driving me crazy! Recently got some compile errors from these added "using" statements (which were all greyed out as unnecessary), and upon review, I knew I had never added them myself. I'd rather have an error prompt about a missing "using" than having these unwanted lines added to my code without my knowledge.