Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Alternatives to obsolete methods?

Discussion in 'Scripting' started by Claytonious, Feb 16, 2009.

  1. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Hello, Uniteers!

    Is there a recommendation regarding the 2 compiler warnings below?

    Code (csharp):
    1. warning CS0618: 'UnityEngine.Quaternion.SetEulerAngles(float, float, float)' is obsolete: 'Use Quaternion.Euler instead. This function was deprecated because it uses radians instad of degrees'
    2. warning CS0618: 'UnityEngine.Component.active' is obsolete: '"the active property is deprecated on components. Please use gameObject.active instead. If you meant to enable / disable a single component use enabled instead."'
    3.  
    In C#, there doesn't seem to be anything called "Euler" as a member of Quaternion. There is a public member called "eulerAngles" but it's read-only. So how do we get at the recommended "Quaternion.Euler" from C#?

    Similarly, there is no property or member called "enabled" exposed from Component (that C# can see). What should we use?

    In both cases, we've made everything work fine in spite of the warnings, but I never like to live with warnings for any longer than necessary so I would like to find out the right way to do these things.

    Thanks very much for any enlightenment!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Quaternion isn't C#, it's a Unity thing which applies to all languages. Look up Quaternion.Euler in the docs; it's there. :)

    Any components can be enabled or disabled...renderer.enabled = false, collider.enabled = false, etc.

    --Eric
     
  3. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Thanks, Eric.

    Yes, I'm very familiar with quaternions and euler angles in general. What I was saying is that from the perspective of C# (which is what is exposed from UnityEngine.dll), there is not a member called "Euler" exposed from the Quaternion class. I assume there is such a thing in Javascript as the docs indicate, but not on the Quaternion .net class that is exposed from UnityEngine.dll.

    Ditto for "enabled" on components such as Collider and WheelCollider. There is no public member called "enabled", only "active", which is marked as obsolete.

    I see that these are in the documentation and assume that they are available from Javascript, but they aren't from C#. Here is everything that Component exposes:



    "Enabled" isn't there.

    Maybe I'm referencing a bad version of UnityEngine.dll? Even so, the obsolete warning refers to it... I think this is the latest from the currently public web player, but I will check...
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Everything that is in the Unity API is language-agnostic; nothing is limited to one language or another. It would be kind of absurd for a warning to tell you to use something that you can't. ;)

    Javascript:
    Code (csharp):
    1. var foo = Quaternion.Euler(Vector3.one);
    C#:
    Code (csharp):
    1. Quaternion foo = Quaternion.Euler(Vector3.one);
    If it's in the docs, you can use it, so yeah, I expect you do have a bad version of UnityEngine.dll.

    --Eric
     
  5. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Not true, unfortunately. For example:

    Code (csharp):
    1. WheelCollider collider = component as WheelCollider;
    2. if (collider != null)
    3.     collider.enabled = false;
    4.  
    gives:

    Code (csharp):
    1. error CS1061: 'UnityEngine.WheelCollider' does not contain a definition for 'enabled' and no extension method 'enabled' accepting a first argument of type 'UnityEngine.WheelCollider' could be found (are you missing a using directive or an assembly reference?)
    However, thanks to your help I did find Euler on the Quaternion class; it's a static method so that does work exactly as you posted. Thanks for that!

    This is not the end of the world because .active seems to do the right thing (at least for this release), but it's uncomfortable having the warning about using an obsolete property (it might go away on the next release?)
     
  6. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,075
    Not everything has an enabled member.

    If you look at the documentation, there's no "enabled" definition for Component:
    http://unity3d.com/support/documentation/ScriptReference/Component.html

    It's defined individually for Behaviour:
    http://unity3d.com/support/documentation/ScriptReference/Behaviour.html
    or for Renderer:
    http://unity3d.com/support/documentation/ScriptReference/Renderer.html

    It's not available, however, for Collider or RigidBody:
    http://unity3d.com/support/documentation/ScriptReference/Collider.html
    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html

    In case of colliders, the easiest way is to enable isTrigger if you want to disable collisions.
     
  7. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    That makes sense. Thank you very much for the reply!