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. Dismiss Notice

Can i call RPC functions outside of MonoBehaviour?

Discussion in 'Scripting' started by jister, Jan 12, 2015.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I'd like an implemented interface to call upon RPC functions, is that possible?
    ie I have a GameManager with a state machine which switches IPlayerInterface from IStartMenu to IGameLobby to...
    I'd like to handle the RPC's in the Children of IPlayerInterface instead of flooding my GameManager with all RPC functions for the game...

    I know i need a networkview attached to call RPC's but is there some way i can start the call from the current implemented IPlayerInterface...?
     
    Last edited: Jan 12, 2015
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    They have to be methods on a MonoBehaviour attached to the GameObject with the networkView. Unity can't find them otherwise.

    That being said - you don't have to put them all in a single file.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    yes i was hoping i could implement then through inheritance... to bad :-(
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    AFAIK this works
    Code (csharp):
    1.  
    2. public class A : MonoBehaviour
    3. {
    4.     [RPC]
    5.     public virtual void Do() { }
    6. }
    7.  
    8. public class B : A
    9. {
    10.     [RPC]
    11.     public override void Do() { base.Do(); }
    12. }
    13.  
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ah yes like that would work... problem is i was trying it with an interface.