NetworkObject & NetworkBehaviour
GameObjects
, NetworkObjects
and NetworkBehaviour
are not serializable types so they cannot be used in RPCs
or NetworkVariables
by default.
There are two convenience wrappers which can be used to send a reference to a NetworkObject
or a NetworkBehaviour
over RPCs or NetworkVariables
.
#
NetworkObjectReferenceNetworkObjectReference
can be used to serialize a reference to a NetworkObject
. It can only be used on already spawned NetworkObjects
.
Here is an example of using NetworkObject
reference to send a target NetworkObject
over an RPC:
#
Implicit OperatorsThere are also implicit operators which convert from/to NetworkObject/GameObject
which can be used to simplify code. For instance the above example can also be written in the following way:
note
The implicit conversion to NetworkObject
/ GameObject
will result in Null
if the reference can not be found.
#
NetworkBehaviourReferenceNetworkBehaviourReference
works similar to NetworkObjectReference
but is used to reference a specific NetworkBehaviour
component on a spawned NetworkObject
.
#
How NetworkObjectReference & NetworkBehaviourReference workNetworkObjectReference
and NetworkBehaviourReference
are convenience wrappers which serialize the id of a NetworkObject
when being sent and on the receiving end retrieve the corresponding
with that id. NetworkBehaviourReference
sends an additional index which is used to find the right NetworkBehaviour
on the NetworkObject
.
Both of them are structs implementing the INetworkSerializable
interface.