Struct NetworkObjectReference
A helper struct for serializing NetworkObjects over the network. Can be used in RPCs and NetworkVariable<T>.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
public struct NetworkObjectReference : INetworkSerializable, IEquatable<NetworkObjectReference>
Constructors
NetworkObjectReference(NetworkObject)
Creates a new instance of the NetworkObjectReference struct.
Declaration
public NetworkObjectReference(NetworkObject networkObject)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | networkObject | The NetworkObject to reference. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
ArgumentException |
NetworkObjectReference(GameObject)
Creates a new instance of the NetworkObjectReference struct.
Declaration
public NetworkObjectReference(GameObject gameObject)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | The GameObject from which the NetworkObject component will be referenced. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException | |
ArgumentException |
Properties
NetworkObjectId
The NetworkObjectId of the referenced NetworkObject.
Declaration
public ulong NetworkObjectId { get; }
Property Value
Type | Description |
---|---|
ulong |
Methods
Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj |
Returns
Type | Description |
---|---|
bool |
Overrides
Equals(NetworkObjectReference)
Declaration
public bool Equals(NetworkObjectReference other)
Parameters
Type | Name | Description |
---|---|---|
NetworkObjectReference | other |
Returns
Type | Description |
---|---|
bool |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int |
Overrides
NetworkSerialize<T>(BufferSerializer<T>)
Provides bi-directional serialization to read and write the desired data to serialize this type.
Declaration
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
Parameters
Type | Name | Description |
---|---|---|
BufferSerializer<T> | serializer | The serializer to use to read and write the data. |
Type Parameters
Name | Description |
---|---|
T | Either BufferSerializerReader or BufferSerializerWriter, depending whether the serializer is in read mode or write mode. |
TryGet(out NetworkObject, NetworkManager)
Tries to get the NetworkObject referenced by this reference.
Declaration
public bool TryGet(out NetworkObject networkObject, NetworkManager networkManager = null)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | networkObject | The NetworkObject which was found. Null if no object was found. |
NetworkManager | networkManager | The networkmanager. Uses Singleton to resolve if null. |
Returns
Type | Description |
---|---|
bool | True if the NetworkObject was found; False if the NetworkObject was not found. This can happen if the NetworkObject has not been spawned yet. you can try getting the reference at a later point in time. |
Operators
implicit operator NetworkObjectReference(NetworkObject)
Implicitly convert NetworkObject to NetworkObjectReference.
Declaration
public static implicit operator NetworkObjectReference(NetworkObject networkObject)
Parameters
Type | Name | Description |
---|---|---|
NetworkObject | networkObject | The NetworkObject to convert from. |
Returns
Type | Description |
---|---|
NetworkObjectReference | The NetworkObjectReference created from the NetworkObject parameter |
implicit operator NetworkObject(NetworkObjectReference)
Implicitly convert NetworkObjectReference to NetworkObject.
Declaration
public static implicit operator NetworkObject(NetworkObjectReference networkObjectRef)
Parameters
Type | Name | Description |
---|---|---|
NetworkObjectReference | networkObjectRef | The NetworkObjectReference to convert from. |
Returns
Type | Description |
---|---|
NetworkObject | The NetworkObject the NetworkObjectReference is referencing |
implicit operator GameObject(NetworkObjectReference)
Implicitly convert NetworkObjectReference to GameObject.
Declaration
public static implicit operator GameObject(NetworkObjectReference networkObjectRef)
Parameters
Type | Name | Description |
---|---|---|
NetworkObjectReference | networkObjectRef | The NetworkObjectReference to convert from. |
Returns
Type | Description |
---|---|
GameObject | This returns the GameObject that the NetworkObject is attached to and is referenced by the NetworkObjectReference passed in as a parameter |
implicit operator NetworkObjectReference(GameObject)
Implicitly convert GameObject to NetworkObject.
Declaration
public static implicit operator NetworkObjectReference(GameObject gameObject)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | The GameObject to convert from. |
Returns
Type | Description |
---|---|
NetworkObjectReference | The NetworkObjectReference created from the GameObject parameter that has a NetworkObject component attached to it |