Suppressing Warnings in Unity

Are you getting warnings stating pretty obvious/useless things? For example, you may be using [SerializeField] tag a lot. In that case, you may get this:

Assets\Scripts\WarningTest.cs(6,24): warning CS0649: Field 'WarningTest.testing' is never assigned to, and will always have its default value null

Well, I have great news for you! There are two ways to suppress such useless warnings!

The first one is to use #pragma in the relevant script. Use #pragma warning disable line before the line that gives a warning, and #pragma warning restore after.

using UnityEngine;

public class WarningTest : MonoBehaviour
{
#pragma warning disable 0649
    [SerializeField]
    private GameObject testing;
    [SerializeField]
    private GameObject testing_2;
    [SerializeField]
    private GameObject testing_3;
#pragma warning restore 0649

    private void Start()
    {
        if (testing)
            Debug.Log("Testing...");
    }
}

You can see the warning id in the console. For out case the id is 0649.

This works fine for individual scripts. But adding these lines to each script can be annoying. Especially if the project is big. And here comes the second solution!

Create a new text file to the Assets folder, and call it csc.rsp. Open it up and add the following line for each warning you don’t want to see:

-nowarn:0649

Just like before, 0649 is the warning id you can see in the console. And that is it! You don’t have to clear the console after each recompile!

4 thoughts to “Suppressing Warnings in Unity”

  1. For this particular warning, you can also assign the private serialized fields to default, e.g.
    private GameObject testing = default;

    1. Oh, interesting! It does make sense, I will try it out. Thanks for the suggestion! 🙂

Comments are closed.