Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Suppressing warnings?

Discussion in 'Scripting' started by matthewminer, Jan 28, 2010.

  1. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    Is it possible to suppress warnings, or perhaps implement a try/catch system of some sort for them?
     
  2. tm

    tm

    Joined:
    Aug 2, 2010
    Posts:
    63
    I also am interested in the answer to this question. Are there compiler directives for warning suppression?
     
  3. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    #pragma warning disable xxxx
    #pragma warning restore xxxx

    Replace xxxx with the warning number to make sure that other types of warnings can still pop up should they appear. If you do not include the xxxx it will disable all warnings until restored.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    or just use the log callback and discard the stuff
     
  5. tm

    tm

    Joined:
    Aug 2, 2010
    Posts:
    63
    useful info.

    I'm not sure what OP was after, but I was looking more for something like the @ prefix in php, where you can suppress a specific instance of a warning.

    I'd like to be able to do things that result in warnings, but I would also like to account for them so that I can look at each particular warning and see if what I'm doing is sane.
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    The Pragma can be used for that.

    Read the Warning number off the warning in the Console.

    This disable the warning right before the line that throws it and enable it again right after.

    It will still throw other errors should they occur if you provide only that specific number.

    like this:
    Code (csharp):
    1.  
    2. #pragma warning disable 1234
    3. int x = true;
    4. #pragma warning restore 1234
    5.  
    Where the line in the middle would throw warning type 1234.
     
  7. tm

    tm

    Joined:
    Aug 2, 2010
    Posts:
    63
    Fantastic. You are a gentleman and a scholar.
     
  8. SixTimesNothing

    SixTimesNothing

    Joined:
    Dec 26, 2007
    Posts:
    167
    Is it possible to suppress warnings in UnityJS too or only in Unity C# using the above code?

    I'm trying to suppress all the 'private method * is never used' warnings on complile. They get really annoying...
     
  9. tm

    tm

    Joined:
    Aug 2, 2010
    Posts:
    63
    I was actually wondering the same thing. I'd only recently tried to plug this into UnityScript, but no joy.
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    its a pragma, they commonly work in UnityScript too, not just C#
     
  11. tm

    tm

    Joined:
    Aug 2, 2010
    Posts:
    63
    You would think that. At least I did. But using them in UnityScript doesn't work for me. Only C#.

    Getting this:

    BCE0043: Unexpected token: ;.
    for the statement on the previous line. I am suspecting at this point that the pragmas may only be valid at the beginning of the script? C# I can drop them around anything with class scope.
     
  12. Aubrey-Falconer

    Aubrey-Falconer

    Joined:
    Feb 13, 2008
    Posts:
    438
    I too am failing to get JS #pragma warning directives working in Unity 3.3.

    Any ideas?
     
  13. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    I want to bump this topic.

    Is there any way to disable specified warnings in UnityScript ?

    pragma directive gives a error
     
    Cawas likes this.
  14. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    I think there's none, up to this day.
     
  15. freshingrain

    freshingrain

    Joined:
    May 31, 2015
    Posts:
    5
    I use "Disable Logging" plugin to enable / disable unity Debug.Log. It's very easy to use.

    Details: http://u3d.as/content/tiinoo/disable-logging

    Screenshot:

     
  16. Deleted User

    Deleted User

    Guest

    Thread is old but since its one of the first in google.
    Now you must create 2 files smcs.rsp(for runtime) and gmcs.rsp(for editor time) or 1 file with name csc.rsp and add in them codes of the warnings you need. In this format -nowarn:0649
    These files must be in Assets folder
     
    Last edited by a moderator: Mar 6, 2020
    wardean and jerome_play like this.
  17. AltaNox

    AltaNox

    Joined:
    Jun 23, 2020
    Posts:
    3
    What about warnings that seem to not have a warning number? For example: missing behaviours, box colliders with negative sizes and animator not playing an animator controller warnings. How do I disable those?
     
    fulgorr and amazingamazing like this.
  18. FeedMyKids1

    FeedMyKids1

    Joined:
    May 17, 2017
    Posts:
    5
    Thank you guys for this thread.

    Ntero saved my console.
     
  19. MEMOMEM

    MEMOMEM

    Joined:
    Feb 26, 2022
    Posts:
    13
    Hey Guys, besides the Pragma-solution, there should be a another approach:

    create an empty text-file in the asset root folder, call it "csc.rsp," and populate it with the warning codes that you want to be suppressed (e.g. the warning "CS0618" would be ignored by typing "-nowarn:0618" in the csc.rsp file), at least from my understanding (here is a link https://www.anton.website/suppressing-warnings-in-unity/).

    However, if I do exactly that, the warnings are not being suppressed.

    I have this warning:
    "Assets/Audio/dearVR/Components/DearVRManager.cs(101,25): warning CS0618: 'WWW' is obsolete: 'Use UnityWebRequest, a fully featured replacement which is more efficient and has additional features'"

    I have this file: Assets/csc.rsp.txt

    and the content of the csc.rsp.txt is only this:
    -nowarn:0618
    -nowarn:0168

    Does anyone have a clue?

    I am on macOS Monterrey
    HDRP Version 12.1.7
    Unity Version 2021.2.11f1

    Thanks in advance!
     
  20. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Remove the .txt-extension.
     
    Bunny83 likes this.
  21. MEMOMEM

    MEMOMEM

    Joined:
    Feb 26, 2022
    Posts:
    13
    Thank you thank you thank you!