Search Unity

Rpc not working if the component is added at runtime

Discussion in 'Netcode for GameObjects' started by geo-bontognali, Nov 1, 2021.

  1. geo-bontognali

    geo-bontognali

    Joined:
    May 19, 2016
    Posts:
    2
    Hi there!

    I discovered a problem implementing some Rpc calls. I'm not sure if I'm maybe just doing things incorrectly or if this is a bug.

    I have a GameObject with the NetworkObject & NetworkTransform Scripts on it. Later at runtime I add a custom Script (NetworkBehaviour) with some RpcCalls in it via gameobject.AddComponent<MyComponent>(); When I try to execute those Rpc it fails with the error: "InvalidCastException: Specified cast is not valid."

    If I add my Script to the Prefab so that it gets spawned with my component already on it, then it works with no issues.

    Is there something more I have to do to register my Rpcs if I add the component later or is this a bug?

    Thanks!



     
  2. TheCaveOfWonders

    TheCaveOfWonders

    Joined:
    Mar 2, 2014
    Posts:
    27
    geo-bontognali likes this.
  3. geo-bontognali

    geo-bontognali

    Joined:
    May 19, 2016
    Posts:
    2
    I see. Thank you for pointing out that thread!