Rpc not working if the component is added at runtime

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(); 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!

I don’t think you can currently add a new NetworkBehaviour AFTER you spawned your object.

1 Like

I see. Thank you for pointing out that thread!