I have a situation where I would want two of the same components( in a hierarchy ), to be on a GameObject. If this component has an RPC method, it obviously cannot change name per instance of the component.

Which RPC method will be called? Unity says this:

"if two RPC functions in different scripts have the same name only one of them is called when RPC is invoked"

This says nothing about which will actually be called. Does Unity define a behavior for when this happens? Is there any alternatives that will avoid this?

  • 1
    \$\begingroup\$ This probably depends on internal compiler details. Usually the reason for not saying things like this is so they can change the behaviour if they want. If you state what will happen then future versions will have to honour that, even if it gets in the way of optimizing. \$\endgroup\$ – Elva Dec 4 '14 at 8:03

Other than to specify that "only one" is called, Unity does not guarantee any behavior in this case. The most likely scenario, which you can determine through experimentation, is that either the first RPC that becomes registered/known to the system is called, or the last one is. However, this is an implementation detail.

Unity explicitly does not want you to care about which function is called in the scenario you're describing; there are several potential reasons, including:

  • This could be because their implementation of the RPC mechanism trades efficiency of dispatch for the inability to deterministically resolve overloaded RPC names.
  • Alternatively, this allows them the flexibility to change their implementation in the future without worrying about violating an API constraint.
  • It's kind of a bad habit to have two RPC methods with exactly the same name in the scene, as it complicates things and makes it harder to reason about execution flow. By not providing a guarantee they are discouraging you from this behavior.
|improve this answer|||||
  • \$\begingroup\$ Unfortunately, if there's two components on a single game object that have a common base class with a single RPC, this problem is going to be encountered. I created an alternative that searches for the correct component and uses reflection to call the method. I feel this is a HUGE problem with Unity's networking. Anyway, your answer answered the question. Thanks. \$\endgroup\$ – Ben Dec 15 '14 at 20:13
  • \$\begingroup\$ Is it not possible to use the FQN of the RPC name (I've never actually used the mechanism)? That's unfortunate. \$\endgroup\$ – user1430 Dec 15 '14 at 20:21
  • \$\begingroup\$ Perhaps, haven't tried it. I'll try it out though. It might be a much better solution than what I've got right now. Presumably RPC just uses System.Reflection, so it should work. \$\endgroup\$ – Ben Dec 16 '14 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.