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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Interface issue on iOS

Discussion in 'iOS and tvOS' started by AChiTenshi, Feb 11, 2014.

  1. AChiTenshi

    AChiTenshi

    Joined:
    Feb 11, 2014
    Posts:
    4
    I've been trying to track down an issue that only occurs on iOS where functions declared in an interface will not be called correctly through an interface reference. So far I have not been able to reproduce this in a simple case and I am pretty sure I must be using some limitation of AOT compilation that is not throwing any message or error. My code works fine in the Unity Editor and in an OS X build.

    I have the following sort of setup (though IWalkingAgent has more to it and CreatureAgent implements a few other interfaces.)

    Code (csharp):
    1. public interface IWalkingAgent : IMapObject
    2. {
    3.     int getMoveCost();
    4. }
    5.  
    6. public class CreatureAgent : MonoBehaviour, IWalkingAgent
    7. {
    8.     public int getMoveCost()
    9.     {
    10.         Debug.log("Move cost requested.")
    11.         return 10;
    12.     }
    13. }
    The behaviour I am getting right now occurs when the second CreatureAgent is created through Resource.load and it behaves as follows:

    Code (csharp):
    1. CreatureAgent agent = newInstance.getComponent<CreatureAgent>();
    2. IWalkingAgent walkingAgent = agent as IWalkingAgent;
    3.  
    4. //The following line will not print out the Debug message and will return a random number upwards of 140000000.
    5. int walkingAgentMoveCost = walkingAgent.getMoveCost();  
    6.  
    7. //The following line however, will correctly print out the Debug message and will return the correct number of 10.
    8. int creatureAgentMoveCost = agent.getMoveCost();
    For some reason right now this only occurs on the second instance of the CreatureAgent. The first one, created using the same code, will work correctly. This issue however has moved several times, usually when I introduce code to try and track it down. I have seen it occur on the first instance of a CreatureAgent when trying to set an event delegate. The event was still null after the line in which the delegate was added to it.

    I have yet to see any error being thrown when this issue occurs, nor have I been able to see a warning that I think is related. (The only warnings I've seen have been referring to the NCalc library, which I have removed temporarily, and onMouseUp being used.)

    I have only been able to find one other post on this issue happening on StackOverflow: http://stackoverflow.com/questions/...tion-using-interface-not-working-in-unity-ios
    Though there was no resolution.

    Has anyone encountered this sort of issue before? Have I missed a post somewhere? Any help would be appreciated.
     
  2. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    Maybe you have to declare the functions as virtual / override to make the polymorphism work as expected.
     
  3. AChiTenshi

    AChiTenshi

    Joined:
    Feb 11, 2014
    Posts:
    4
    Thanks for the suggestion.

    I'll double check but I believe those particular methods are not being overridden anywhere. They are just being implemented off the interface. I'll give it a try and let you know what happens.

    Though I did find the line of code that seemed to be causing the issue, but its more likely that removing it just moved the issue to someplace I have not found unless anyone can explain to me why the following line would cause chaos.

    Code (csharp):
    1.  
    2. //agent in this case is a reference to a CreatureAgent
    3. Debug.Log("Creature that died: " + agent.toString());
    4.  
    I had that line of code so I could see what was happening on device, before I figured out how to do remote script debugging. After removing it, the issue I described above seems to have gone away. If it was that line it makes sense as to why the issue was moving around as I was doing that in different areas of the code to try and track the issue down, but I still don't see how a toString method could cause it.

    Either way I'll put the line back and see if there is anyplace I may have missed placing an override or virtual though Unity seems to warn me about hiding methods if I forget them.

    Thanks again.
     
  4. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    Actually YOU have to declare this function as virtual (on IWalkingAgent) and override (on CreatureAgent) if you want polymorphism to work.
     
  5. AChiTenshi

    AChiTenshi

    Joined:
    Feb 11, 2014
    Posts:
    4
    I think I need a bit of clarification on your post, perhaps an example.

    From what I understand from your post you want me to add the 'virtual' keyword in front of the interface method which isn't actually allowed by the complier. Likewise the 'override' keyword is not allowed in front of an implemented interface method as as the complier cannot locate a function to override.
     
  6. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    There is known AOT bug when calling methods via interfaces. It is expected to be fixed with next Unity release. Currently we suggest to avoid calling via interfaces (abstract classes should be ok).
     
  7. AChiTenshi

    AChiTenshi

    Joined:
    Feb 11, 2014
    Posts:
    4
    Thanks, I'll work around it until then.
     
  8. MarcHanson

    MarcHanson

    Joined:
    Mar 20, 2014
    Posts:
    1
    What is the estimated date of the release that contains the fix?
     
  9. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    It is approaching release candidate phase, but since it also major release, it usually takes more time to mature.
     
  10. tayl0r

    tayl0r

    Joined:
    Jan 6, 2012
    Posts:
    85
    This sounds like an issue that isn't happening 100% of the time- what other factors would cause it?

    Because myself and other developers I know are calling methods via interfaces and we aren't running into any problems.

    Is it specifically related to doing the GetComponent<CreatureAgent>() as <IWalkingAgent> ?
     
  11. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    This happens when many interface methods collide into single IMT table slot (similar to hashtable collision) and you are trying to invoke method, which belongs to this overcrowded IMT slot. Adding/removing extra code on unrelated site might change the things.
     
  12. holyfuzz

    holyfuzz

    Joined:
    Nov 16, 2010
    Posts:
    21
    My company is also having this issue, and it's blocking us from releasing our game on iOS. (A lot of our core gameplay logic uses interfaces, as well as several third-party libraries over which we have little control.) I've had a hard time getting the game playable without removing major features, which is of course unacceptable.

    Perhaps Unity could fix this bug ASAP via one of its new patch-releases?
     
  13. QA-for-life

    QA-for-life

    Unity Technologies

    Joined:
    Dec 23, 2011
    Posts:
    89
    This bug is fixed in 4.5 and as that is already in Release Candidate 4 it is going to be out very soon. Quite possibly before we would be able to backport the fix and ship it in a 4.3 patch, so we're probably going to let that stay as a 4.5 fix.
     
  14. Velvety

    Velvety

    Joined:
    May 4, 2009
    Posts:
    150
    Hi, we are experiencing this bug as well and have a major iOS release in 2 weeks. The amount of interfaces we are using in our project (including plugins from the Asset Store) is not minimal and refactoring them all is not a feasible option at this stage. Are you able to provide us with a more accurate estimate of when the fix will be available in 4.5? Thanks
     
  15. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,864
  16. Velvety

    Velvety

    Joined:
    May 4, 2009
    Posts:
    150
    So it appears, thank you :)