Search Unity

Solving Exception "Missing method" with an extension.

Discussion in 'Scripting' started by vincespeed, Jul 2, 2019.

  1. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    Hello guys,
    I had to update a project from Unity 5 to Unity 2018. Unfortunately, the project uses 3rd part dlls and here comes my issue.
    In those dlls, somewhere, there are implementations of "Particle Emitter" (Deprecated). From time to time, I get these two errors in the log:
    "Exception Missing Method UnityEngine.ParticleEmitter.get_autodestruct"
    "Exception Missing Method UnityEngine.ParticleEmitter.set_maxSize".
    To get rid of them, I thought I could write an extension for ParticleEmitter. Which is the following:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class extendParticleEmitter {
    6.  
    7.     public static void set_maxSize(this ParticleEmitter pe,double g){
    8.         g = 0;
    9.     }
    10.     public static void get_autodestruct(this ParticleEmitter pe){
    11.         int g = 0;
    12.     }
    13.  
    While I can verify these two functions now exist, by writing in my code things like "GetComponent<ParticleEmitter>().set_maxSize"....
    It doesn't seem to work, as I keep getting the "Missing Method" exceptions. Am I doing something wrong, or I simply cannot extend the Particle Emitter?
    Thanks for your help :)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Extension methods don't actually add new methods to an object, so your clever idea unfortunately isn't going to work.
     
  3. YetAnotherKen

    YetAnotherKen

    Joined:
    Jun 17, 2019
    Posts:
    30
    If you extend an object you just have to make sure that you specifically call the method by it's full name so that you don't keep trying to use the original method. Something like
    Code (csharp):
    1. <namespace>.extendParticleEmitter.set_maxSize(<parms);
    You'll have to read up on the syntax for class extension, it doesn't look at all correct to me in the sample code you provided.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The extensions cannot work, as the extension methods you're using are not identical to the methods that the plugin might want to access internally. The plugin doesn't even know these extensions methods, as its code is not dependent on yours.

    If you want to stick to the newer version that you upgraded to, your best bets are
    1) contact the developers of that plugin and kindly ask for a different version
    2) switch to another plugin that gets the job done.
    3) try to check whether the offending types / components in that plugin are necessary in order to use the plugins features

    If you're the one that calls into the plugin, you're technically able to catch those exceptions. But honestly, that's probably the worst thing you could do, as the plugin might no longer work as expected if anything important happens after the offending lines were hit. So, even though that sounds like an option, it's definitely not recommended.
     
  5. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    I see your point, guys, thank you!
    One never stops learning new things :D
    I'll get in touch with the plugin's dev and see if he can sort this matter out. Meanwhile, exception apart, his plugin seems to work flawlessly. It only spits these exceptions in the editor...
    Again, thank you for your explanations, I really appreciate your help!