Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity3D ignores the System.Obsolete attribute

Discussion in 'Scripting' started by phaem, Jan 20, 2015.

  1. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    even when error flag is set to True. No warning, no error - just empty console after I recompiled the code, where my component class is attributed with System.Obsolete. Why? How to fix that?
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Can you show the corresponding code?
     
  3. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    Sure, here it is

    Code (CSharp):
    1.     [System.Obsolete("Prototype code for adjusting camera look, should not used in production.", true)]
    2.     public class CameraLook : MonoBehaviour
    3.     {
    4.         // some code here...
    5.     }
    6.  
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Hmmm... And how does the code look that tries to use your obsolete CameraLook class? Can you prepare a small unity project that I can test on my own pc?
     
  5. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    It is used as camera component. It is as simple as calling to transform.LookAt in LateUpdate. No code using it exists in other components. I understand what compiler will only fire a warning or error if the obsolete piece of code is used somewhere else in codebase and it is not. I would like to know why Unity itself ignores the obsolete attribute. I expected a warning about obsolete component used or even a error if flag is set. But nothing happened.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Just to check, have you toggled off warnings in the console?
     
  7. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    No, the flag is on. I tried setting error flag of the attribute to True, as you can see in the example above, still nothing.
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    No i dont mean the flag, i mean the console itself has toggles
     
  9. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    I got that :) Yes, it is always on for me. I do not tolerate warnings in my code.
     
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Ok, I think you're just expecting something to happen that never will.

    The following works:

    Code (CSharp):
    1. //Obsolete monobehaviour
    2. [System.Obsolete( "Dont use component" )]
    3. public class ObsoleteClass : MonoBehaviour {
    4.   ///...
    5. }
    6.  
    7.  
    8. //Some other class that has code to use obsolete class
    9. public class ClassAdder: MonoBehaviour {
    10.     void OnGUI () {
    11.         if ( GUILayout.Button( "Add it" ) ) {
    12.             gameObject.AddComponent<ObsoleteClass>();
    13.             //When compiled, this gives the obsolete warning
    14.         }
    15.     }
    16. }
    17.