Conditional Compilation!

Is there something like #if…#endif in Unity C#? I want to keep my Debug.Line( ), Debug.Write( ), etc. in the code only on debug.

http://unity3d.com/support/documentation/Manual/Platform%20Dependent%20Compilation.html

Should be able to say

#if UNITY_EDITOR
Debug.Log();
#endif

There is platform dependent compilation in Unity but as I am aware you can’t define your own symbols.

But for your specific needs, you could create a class wrapper around Debug.X() methods.

Here’s an example class:

public class Logger
{
    private static Logger Instance = null;
    private bool enabled = false;

    public Logger()
    {
    }

    public bool Enable
    {
        set { enabled = value; }
    }

    public Logger Instance
    {
        get
        {
            if (instance == null)
                instance = new Logger();

            return instance;
        }
    }

    public void Log(object message)
    {
        if (enabled)
            Debug.Log(message);
    }
}

Of course, you would have to set enabled to true/false when you need the logging enabled.

If you prefer having a class inheriting MonoBehaviour, you would benefit from having the ability to preset logging properties such as “enabled” or any other extra setting you might think of (for example: displaying only errors) from Inspector. But you would have to ensure the object carrying this script is not destroyed (put in in the 1st scene) and that there’s no logging in the Awake() methods unless you are sure the Logger script is executed before others.

public class Logger : MonoBehaviour
{
    public static Logger Instance = null;
    public bool Enabled = false;

    void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(this);
    }

    public void Log(object message)
    {
        if (Enabled)
            Debug.Log(message);
    }
}

You would be able to use any of these classes from wherever in your project in the same way:

Logger.Instance.Log("message");

I haven’t tested any of this code, it might even not compile, but you’ll get the idea.

Guys, thank you so much for your help. This is wonderful. Thanks again.