Class NetworkObject
A component used to identify that a GameObject in the network
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.Netcode.Runtime.dll
Syntax
[AddComponentMenu("Netcode/Network Object", -99)]
[DisallowMultipleComponent]
public sealed class NetworkObject : MonoBehaviour
Fields
ActiveSceneSynchronization
When set to true and the active scene is changed, this will automatically migrate the Network
Declaration
public bool ActiveSceneSynchronization
Field Value
Type | Description |
---|---|
bool |
Remarks
- This only applies to dynamically spawned Network
Object s. - This only works when using integrated scene management (Network
Scene ).Manager
If there are more than one scenes loaded and the currently active scene is unloaded, then typically
the Scene
AlwaysReplicateAsRoot
If true, the object will always be replicated as root on clients and the parent will be ignored.
Declaration
public bool AlwaysReplicateAsRoot
Field Value
Type | Description |
---|---|
bool |
AutoObjectParentSync
Whether or not to enable automatic NetworkObject parent synchronization.
Declaration
public bool AutoObjectParentSync
Field Value
Type | Description |
---|---|
bool |
CheckObjectVisibility
Delegate invoked when the netcode needs to know if the object should be visible to a client, if null it will assume true
Declaration
public NetworkObject.VisibilityDelegate CheckObjectVisibility
Field Value
Type | Description |
---|---|
Network |
DontDestroyWithOwner
Whether or not to destroy this object if it's owner is destroyed. If true, the objects ownership will be given to the server.
Declaration
public bool DontDestroyWithOwner
Field Value
Type | Description |
---|---|
bool |
IncludeTransformWhenSpawning
Delegate invoked when the netcode needs to know if it should include the transform when spawning the object, if null it will assume true
Declaration
public NetworkObject.SpawnDelegate IncludeTransformWhenSpawning
Field Value
Type | Description |
---|---|
Network |
OnMigratedToNewScene
Notifies when the NetworkObject is migrated into a new scene
Declaration
public Action OnMigratedToNewScene
Field Value
Type | Description |
---|---|
Action |
Remarks
- Active
Scene or SceneSynchronization Migration (or both) need to be enabledSynchronization - This only applies to dynamically spawned Network
Object s. - This only works when using integrated scene management (Network
Scene ).Manager
SceneMigrationSynchronization
When enabled (the default), if a Network
Declaration
public bool SceneMigrationSynchronization
Field Value
Type | Description |
---|---|
bool |
Remarks
- This only applies to dynamically spawned Network
Object s. - This only works when using integrated scene management (Network
Scene ). Note: You can have both ActiveManager Scene and SceneSynchronization Migration enabled. The primary difference between the two is that SceneSynchronization Migration only synchronizes clients when the server migrates a NetworkSynchronization Object to a new scene. If the scene is unloaded and DestroyWith is true and ActiveScene Scene is false and the scene is not the currently active scene, then the NetworkSynchronization Object will be destroyed.
SpawnWithObservers
When set to false, the NetworkObject will be spawned with no observers initially (other than the server)
Declaration
[Tooltip("When false, the NetworkObject will spawn with no observers initially. (default is true)")]
public bool SpawnWithObservers
Field Value
Type | Description |
---|---|
bool |
SynchronizeTransform
Determines if the associated NetworkObject's transform will get synchronized when spawned.
Declaration
public bool SynchronizeTransform
Field Value
Type | Description |
---|---|
bool |
Remarks
For things like in-scene placed NetworkObjects that have no visual components can help reduce the instance's initial synchronization bandwidth cost. This can also be useful for UI elements that have a predetermined fixed position.
Properties
DestroyWithScene
Gets whether or not the object should be automatically removed when the scene is unloaded.
Declaration
public bool DestroyWithScene { get; set; }
Property Value
Type | Description |
---|---|
bool |
IsLocalPlayer
Gets if the object is the personal clients player object
Declaration
public bool IsLocalPlayer { get; }
Property Value
Type | Description |
---|---|
bool |
IsOwnedByServer
Gets Whether or not the object is owned by anyone
Declaration
public bool IsOwnedByServer { get; }
Property Value
Type | Description |
---|---|
bool |
IsOwner
Gets if the object is owned by the local player or if the object is the local player object
Declaration
public bool IsOwner { get; }
Property Value
Type | Description |
---|---|
bool |
IsPlayerObject
Gets if this object is a player object
Declaration
public bool IsPlayerObject { get; }
Property Value
Type | Description |
---|---|
bool |
IsSceneObject
Gets if the object is a SceneObject, null if it's not yet spawned but is a scene object.
Declaration
public bool? IsSceneObject { get; }
Property Value
Type | Description |
---|---|
bool? |
IsSpawned
Gets if the object has yet been spawned across the network
Declaration
public bool IsSpawned { get; }
Property Value
Type | Description |
---|---|
bool |
NetworkManager
Gets the NetworkManager that owns this NetworkObject instance
Declaration
public NetworkManager NetworkManager { get; }
Property Value
Type | Description |
---|---|
Network |
NetworkObjectId
Gets the unique Id of this object that is synced across the network
Declaration
public ulong NetworkObjectId { get; }
Property Value
Type | Description |
---|---|
ulong |
OwnerClientId
Gets the ClientId of the owner of this NetworkObject
Declaration
public ulong OwnerClientId { get; }
Property Value
Type | Description |
---|---|
ulong |
PrefabIdHash
Gets the Prefab Hash Id of this object if the object is registerd as a prefab otherwise it returns 0
Declaration
[HideInInspector]
public uint PrefabIdHash { get; }
Property Value
Type | Description |
---|---|
uint |
Methods
ChangeOwnership(ulong)
Changes the owner of the object. Can only be called from server
Declaration
public void ChangeOwnership(ulong newOwnerClientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | newOwnerClientId | The new owner clientId |
Despawn(bool)
Despawns the Game
Declaration
public void Despawn(bool destroy = true)
Parameters
Type | Name | Description |
---|---|---|
bool | destroy | (true) the Game |
GetNetworkBehaviourAtOrderIndex(ushort)
Declaration
public NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
Parameters
Type | Name | Description |
---|---|---|
ushort | index |
Returns
Type | Description |
---|---|
Network |
GetObservers()
Returns Observers enumerator
Declaration
public HashSet<ulong>.Enumerator GetObservers()
Returns
Type | Description |
---|---|
Hash |
Observers enumerator |
InstantiateAndSpawn(NetworkManager, ulong, bool, bool, bool, Vector3, Quaternion)
Declaration
public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ownerClientId = 0, bool destroyWithScene = false, bool isPlayerObject = false, bool forceOverride = false, Vector3 position = default, Quaternion rotation = default)
Parameters
Type | Name | Description |
---|---|---|
Network |
networkManager | The local instance of the NetworkManager connected to an session in progress. |
ulong | ownerClientId | The owner of the Network |
bool | destroyWithScene | Whether the Network |
bool | isPlayerObject | Whether the Network |
bool | forceOverride | Whether you want to force spawning the override when running as a host or server or if you want it to spawn the override for host mode and the source prefab for server. If there is an override, clients always spawn that as opposed to the source prefab (defaults to false). |
Vector3 | position | The starting poisiton of the Network |
Quaternion | rotation | The starting rotation of the Network |
Returns
Type | Description |
---|---|
Network |
The newly instantiated and spawned Network |
InstantiateAndSpawn(GameObject, NetworkManager, ulong, bool, bool, bool, Vector3, Quaternion)
Declaration
public static NetworkObject InstantiateAndSpawn(GameObject networkPrefab, NetworkManager networkManager, ulong ownerClientId = 0, bool destroyWithScene = false, bool isPlayerObject = false, bool forceOverride = false, Vector3 position = default, Quaternion rotation = default)
Parameters
Type | Name | Description |
---|---|---|
Game |
networkPrefab | The NetworkPrefab to instantiate and spawn. |
Network |
networkManager | The local instance of the NetworkManager connected to an session in progress. |
ulong | ownerClientId | The owner of the Network |
bool | destroyWithScene | Whether the Network |
bool | isPlayerObject | Whether the Network |
bool | forceOverride | Whether you want to force spawning the override when running as a host or server or if you want it to spawn the override for host mode and the source prefab for server. If there is an override, clients always spawn that as opposed to the source prefab (defaults to false). |
Vector3 | position | The starting poisiton of the Network |
Quaternion | rotation | The starting rotation of the Network |
Returns
Type | Description |
---|---|
Network |
The newly instantiated and spawned Network |
IsNetworkVisibleTo(ulong)
Whether or not this object is visible to a specific client
Declaration
public bool IsNetworkVisibleTo(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The clientId of the client |
Returns
Type | Description |
---|---|
bool | True if the client knows about the object |
NetworkHide(List<NetworkObject>, ulong)
Hides a list of Network
Declaration
public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
List<Network |
networkObjects | The Network |
ulong | clientId | The targeted client |
Remarks
Usage: Use to stop sending updates to the targeted client, "netcode invisible", for the currently visible Network
Dynamically Spawned: Network
In-Scene Placed: Network
See Also:
Network
Network
NetworkHide(ulong)
Hides the Network
Declaration
public void NetworkHide(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The targeted client |
Remarks
Usage: Use to stop sending updates to the targeted client, "netcode invisible", for a currently visible Network
Dynamically Spawned: Network
In-Scene Placed: Network
See Also:
Network
Network
NetworkShow(List<NetworkObject>, ulong)
Makes a list of previously hidden Network
Declaration
public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
List<Network |
networkObjects | The objects to become "netcode visible" to the targeted client |
ulong | clientId | The targeted client |
Remarks
Usage: Use to start sending updates for previously hidden Network
Dynamically Spawned: Network
In-Scene Placed: Already instantiated but despawned Network
See Also:
Network
Network
NetworkShow(ulong)
Makes the previously hidden Network
Declaration
public void NetworkShow(ulong clientId)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The targeted client |
Remarks
Usage: Use to start sending updates for a previously hidden Network
Dynamically Spawned: Network
In-Scene Placed: The instantiated but despawned Network
See Also:
Network
Network
RemoveOwnership()
Removes all ownership of an object from any client. Can only be called from server
Declaration
public void RemoveOwnership()
Spawn(bool)
Spawns this Network
Declaration
public void Spawn(bool destroyWithScene = false)
Parameters
Type | Name | Description |
---|---|---|
bool | destroyWithScene | Should the object be destroyed when the scene is changed |
SpawnAsPlayerObject(ulong, bool)
Spawns a Network
Declaration
public void SpawnAsPlayerObject(ulong clientId, bool destroyWithScene = false)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The clientId who's player object this is |
bool | destroyWithScene | Should the object be destroyed when the scene is changed |
SpawnWithOwnership(ulong, bool)
Spawns a Network
Declaration
public void SpawnWithOwnership(ulong clientId, bool destroyWithScene = false)
Parameters
Type | Name | Description |
---|---|---|
ulong | clientId | The clientId to own the object |
bool | destroyWithScene | Should the object be destroyed when the scene is changed |
TryRemoveParent(bool)
Removes the parent of the NetworkObject's transform
Declaration
public bool TryRemoveParent(bool worldPositionStays = true)
Parameters
Type | Name | Description |
---|---|---|
bool | worldPositionStays | If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before. |
Returns
Type | Description |
---|---|
bool |
Remarks
This is a more convenient way to remove the parent without having to cast the null value to either Game
TrySetParent(NetworkObject, bool)
Set the parent of the NetworkObject transform.
Declaration
public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
Parameters
Type | Name | Description |
---|---|---|
Network |
parent | The new parent for this NetworkObject transform will be the child of. |
bool | worldPositionStays | If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before. |
Returns
Type | Description |
---|---|
bool | Whether or not reparenting was successful. |
TrySetParent(GameObject, bool)
Set the parent of the NetworkObject transform.
Declaration
public bool TrySetParent(GameObject parent, bool worldPositionStays = true)
Parameters
Type | Name | Description |
---|---|---|
Game |
parent | The new parent for this NetworkObject transform will be the child of. |
bool | worldPositionStays | If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before. |
Returns
Type | Description |
---|---|
bool | Whether or not reparenting was successful. |
TrySetParent(Transform, bool)
Set the parent of the NetworkObject transform.
Declaration
public bool TrySetParent(Transform parent, bool worldPositionStays = true)
Parameters
Type | Name | Description |
---|---|---|
Transform | parent | The new parent for this NetworkObject transform will be the child of. |
bool | worldPositionStays | If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before. |
Returns
Type | Description |
---|---|
bool | Whether or not reparenting was successful. |
WorldPositionStays()
Returns the last known cached WorldPositionStays value for this NetworkObject
Declaration
public bool WorldPositionStays()
Returns
Type | Description |
---|---|
bool | true or false |
Remarks
When parenting NetworkObjects, the optional WorldPositionStays value is cached and synchronized with clients.
This method provides access to the instance relative cached value.
Try