Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MonoBehavior implement interface with custom Update(float) method error

Discussion in 'Scripting' started by R1PFake, Jan 9, 2021.

  1. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    533
    I have a class (let's say MyBehavior) which uses MonoBehavior as base class and a interface (let's say MyInterface) which has a Update(float) method.

    I want to implement MyInterface in MyBehavior, but the problem is Unity thinks I want to implement the Unity Update(void) method instead of my interface Update(float) method and gives me an error "Update() can not take parameters".

    Is there any way to "tell" Unity that this is the interface Update(float) method and NOT the "magic" Unity Update(void) method, so that Unity ignores it? Maybe with an Attribute or something?

    Btw it doesn't matter if I implement the interface implicit or explicit, I get the same error in both cases.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Just stop trying to fight against the engine. Name your method something else. OnUpdate() is an option.
     
    R1PFake, Bunny83 and Peter77 like this.
  3. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    533
    Doesn't work if the interface is in a .dll and you don't have access to the interface source code.

    As a workaround I made a wrapper class (not a MonoBehavior) which has access to my behavior class and implements the interface.
     
    PraetorBlue likes this.