ConditionalAttribute not working

I am trying to use ConditionalAttribute and it is not working
I noticed that there is a bug here but it says resolved??

Can someone please verify if this is still a bug ?

#define ENABLE_DEBUG_LOGGING

    [ConditionalAttribute("ENABLE_DEBUG_LOGGING")]
    public static void Log (object message)
    {
            UnityEngine.Debug.Log(message);
    }
1 Like

use [Conditional(…)] instead

BTW: The link doesn’t work

1 Like

Thanks for the quick reply!

I fixed link above,

I just tested changing to Conditional and it is still not working.
I copied the code from the msdn link and tested and the Conditional is always evaluating to false and removing the method.

I am using Unity 5.5.3p2

#define CONDITION1

[Conditional("CONDITION1")]
    public static void Method1()
    {
        UnityEngine.Debug.Log("condition1 working");
    }

Hi did you guys ever get this resolved? I can’t get mine to work either.

Mine is working, both with Conditional and ConditionalAttribute.

Make sure you put the ‘#define’ at the top of the file.
Make sure the define symbol is known before your code executes, etc…
small refresher here for anyone interested: https://msdn.microsoft.com/en-us/library/aa664622(v=vs.71).aspx

#define TESTERSYMBL // comment this out for another test.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
public class ConditionalTest : MonoBehaviour {

   // Use this for initialization
   void Start () {
        testcall();
   }
   
    [Conditional("TESTERSYMBL")]
    void testcall()
    {
        print("This was called.");
    }
}

No, doesn’t work for me. I have the #define and also tried adding the using System.Diagnostics statement to the top. this used to all work fine a few versions of Unity ago but at some point it stopped, not sure which version. I’m using 5.6.2 and having the conditional attribute just greys out the calls to these functions, if I comment out the attribute then the functions get called just fine (and are no longer grey).

Have you tried adding the define in the build settings’ “Scripting Define Symbols” ? Although I’d assume it should also work using a standard #define (but I’ve never tried it that way).

1 Like

Using the #define is not working for me on a mac with unity 5.6p1, however, I was able to get this working by adding the variable to the Scripting Define Symbol. It was way better when you could do the #define but I guess this is better than not working at all!

Am on 2017.1 and seeing a variant of this issue.

I submitted a bug to Unity (case 934431) about it; and according to the feedback I got the behaviour of the conditional attribute in Unity versions before 5.5 was incorrect according to the C# standards; and it’s now working correctly.

“Correctly” means that if I have:

//
// code in "DbgLog.cs"
//
static class DbgLog
{
    [Conditional( "CONDITIONAL_LOG_FUNCTION" )]
    private static void ConditionalLog( string strOutput )
    {
        Debug.Log( strOutput );
    }
}

then I would have to do one or both of the following

  • define CONDITIONAL_LOG_FUNCTION in all .cs files in which I want calls to DbgLog.ConditionalLog to have some effect
  • add CONDITIONAL_LOG_FUNCTION to the preprocessor defines in player settings to have calls to DbgLog.ConditionalLog work globally

Obviously what is happening is that if CONDITIONAL_LOG_FUNCTION is not defined in any given .cs file, then all calls to ConditionalLog in that file magically disappear during the compilation process (I believe they’re removed at the MIL → native stage of the compilation process).

I can confirm that both of these approaches work for me.

Alex

5 Likes

Wow, thanks for looking into this. Great to hear how to properly use it :slight_smile:

1 Like

no worries :slight_smile: