Search Unity

Unity 5.5 UnityAction error CS0121. Is this a bug?

Discussion in 'Scripting' started by cecarlsen, Dec 8, 2016.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    522
    Unity 5.5 is not happy about member overloading using UnityAction arguments. The script works perfectly fine in Unity 5.4.3, but in 5.5.0 it generates the following error:

    Assets/Test.cs(7,3): error CS0121: The call is ambiguous between the following methods or properties: `Test.Map(UnityEngine.Events.UnityAction<float>)' and `Test.Map(UnityEngine.Events.UnityAction<int>)

    Is this error intended by UT, or is it a bug in Unity 5.5?

    Same problem for System.Action btw.

    ~ce

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.    void Start(){
    7.       Map( FloatMethod );
    8.       Map( IntMethod );
    9.    }
    10.  
    11.    void FloatMethod( float value ){
    12.       Debug.Log( value );
    13.    }
    14.  
    15.    void IntMethod (int value ){
    16.       Debug.Log( value );
    17.    }
    18.  
    19.    void Map( UnityAction<float> method ){
    20.       method.Invoke( 0f );
    21.    }
    22.  
    23.    void Map( UnityAction<int> method ){
    24.       method.Invoke( 0 );
    25.    }
    26. }
     
  2. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    522
    Thanks for your insight. Wow, if that is the case, it is really S*** news. I have several scripts that break because of this. We may have to wait years for C# 5 to make it into Unity. It would be great to have someone from UT confirm this.

    Meanwhile ...

    I wonder what would be the most graceful workaround?
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    520
    cecarlsen and MV10 like this.
  5. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Well that's interesting... implicit conversions. The clue is in the fact that only the float version is highlighted as ambiguous, since an int can be automatically cast to a float:

    1.jpg

    And of course, it goes away with a type cast. I thought I'd done something like this before but apparently not.

    Code (csharp):
    1. void Start()
    2. {
    3.     Map((Action<float>)FloatMethod);
    4.     Map(IntMethod);
    5. }
     
    cecarlsen likes this.