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

ConditionalAttribute not working

Discussion in 'Scripting' started by phoenixrising, May 6, 2017.

  1. phoenixrising

    phoenixrising

    Joined:
    Aug 9, 2013
    Posts:
    57
    I am trying to use ConditionalAttribute and it is not working
    I noticed that there is a bug here but it says resolved??

    https://issuetracker.unity3d.com/is...-attribute-string-is-not-shown-in-the-console

    Can someone please verify if this is still a bug ?

    Code (CSharp):
    1. #define ENABLE_DEBUG_LOGGING
    2.  
    3.     [ConditionalAttribute("ENABLE_DEBUG_LOGGING")]
    4.     public static void Log (object message)
    5.     {
    6.             UnityEngine.Debug.Log(message);
    7.     }
     
    Last edited: May 6, 2017
    LouisHong likes this.
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  3. phoenixrising

    phoenixrising

    Joined:
    Aug 9, 2013
    Posts:
    57
    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

    Code (CSharp):
    1. #define CONDITION1
    2.  
    3. [Conditional("CONDITION1")]
    4.     public static void Method1()
    5.     {
    6.         UnityEngine.Debug.Log("condition1 working");
    7.     }
     
    Last edited: May 6, 2017
  4. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    Hi did you guys ever get this resolved? I can't get mine to work either.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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
    Code (csharp):
    1.  
    2. #define TESTERSYMBL // comment this out for another test.
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using System.Diagnostics;
    7. public class ConditionalTest : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.    void Start () {
    11.         testcall();
    12.    }
    13.    
    14.     [Conditional("TESTERSYMBL")]
    15.     void testcall()
    16.     {
    17.         print("This was called.");
    18.     }
    19. }
    20.  
     
  6. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    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).
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    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).
     
    lvlzro likes this.
  8. phoenixrising

    phoenixrising

    Joined:
    Aug 9, 2013
    Posts:
    57
    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!
     
  9. darbotron

    darbotron

    Joined:
    Aug 9, 2010
    Posts:
    351
    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 (CSharp):
    1. //
    2. // code in "DbgLog.cs"
    3. //
    4. static class DbgLog
    5. {
    6.     [Conditional( "CONDITIONAL_LOG_FUNCTION" )]
    7.     private static void ConditionalLog( string strOutput )
    8.     {
    9.         Debug.Log( strOutput );
    10.     }
    11. }
    then I would have to do one or both of the following
    1. define CONDITIONAL_LOG_FUNCTION in all .cs files in which I want calls to DbgLog.ConditionalLog to have some effect
    2. 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
     
    Last edited: Aug 2, 2017
    Munchy2007, DSivtsov, lvlzro and 2 others like this.
  10. MythicalCity

    MythicalCity

    Joined:
    Feb 10, 2011
    Posts:
    420
    Wow, thanks for looking into this. Great to hear how to properly use it :)
     
    darbotron likes this.
  11. darbotron

    darbotron

    Joined:
    Aug 9, 2010
    Posts:
    351
    no worries :)