Class NetworkObject
A component used to identify that a GameObject in the network
Inherited Members
Namespace: MLAPI
Assembly: Unity.Multiplayer.MLAPI.Runtime.dll
Syntax
[AddComponentMenu("MLAPI/NetworkObject", -99)]
[DisallowMultipleComponent]
public sealed class NetworkObject : MonoBehaviour
Fields
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 |
CheckObjectVisibility
Delegate invoked when the MLAPI 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 |
|---|---|
| NetworkObject.VisibilityDelegate |
DontDestroyWithOwner
Whether or not to destroy this object if it's owner is destroyed. If false, the objects ownership will be given to the server.
Declaration
public bool DontDestroyWithOwner
Field Value
| Type | Description |
|---|---|
| bool |
IncludeTransformWhenSpawning
Delegate invoked when the MLAPI 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 |
|---|---|
| NetworkObject.SpawnDelegate |
NetworkInstanceId
InstanceId is the id that is unique to the object and scene for a scene object when UsePrefabSync is false. If UsePrefabSync is true or if it's used on non scene objects, this has no effect. Should not be set manually
Declaration
[HideInInspector]
[SerializeField]
public ulong NetworkInstanceId
Field Value
| Type | Description |
|---|---|
| ulong |
PrefabHash
The Prefab unique hash. This should not be set my the user but rather changed by editing the PrefabHashGenerator. It has to be the same for all instances of a prefab
Declaration
[HideInInspector]
[SerializeField]
public ulong PrefabHash
Field Value
| Type | Description |
|---|---|
| ulong |
PrefabHashGenerator
The generator used to change the PrefabHash. This should be set the same for all instances of a prefab. It has to be unique in relation to other prefabs
Declaration
[SerializeField]
public string PrefabHashGenerator
Field Value
| Type | Description |
|---|---|
| string |
Properties
DestroyWithScene
Gets whether or not the object should be automatically removed when the scene is unloaded.
Declaration
public bool DestroyWithScene { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsLocalPlayer
Gets if the object is the 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 |
|---|---|
| NetworkManager |
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 |
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 this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
Declaration
public void Despawn(bool destroy = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | destroy |
GetObservers()
Returns Observers enumerator
Declaration
public HashSet<ulong>.Enumerator GetObservers()
Returns
| Type | Description |
|---|---|
| HashSet<ulong>.Enumerator | Observers enumerator |
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 objects from a client
Declaration
public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientId)
Parameters
| Type | Name | Description |
|---|---|---|
| List<NetworkObject> | networkObjects | The objects to hide |
| ulong | clientId | The client to hide the objects from |
NetworkHide(ulong)
Hides a object from a specific client
Declaration
public void NetworkHide(ulong clientId)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | clientId | The client to hide the object for |
NetworkShow(List<NetworkObject>, ulong, Stream)
Shows a list of previously hidden objects to a client
Declaration
public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientId, Stream payload = null)
Parameters
| Type | Name | Description |
|---|---|---|
| List<NetworkObject> | networkObjects | The objects to show |
| ulong | clientId | The client to show the objects to |
| Stream | payload | An optional payload to send as part of the spawns |
NetworkShow(ulong, Stream)
Shows a previously hidden object to a client
Declaration
public void NetworkShow(ulong clientId, Stream payload = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | clientId | The client to show the object to |
| Stream | payload | An optional payload to send as part of the spawn |
RemoveOwnership()
Removes all ownership of an object from any client. Can only be called from server
Declaration
public void RemoveOwnership()
Spawn(Stream, bool)
Spawns this GameObject across the network. Can only be called from the Server
Declaration
public void Spawn(Stream spawnPayload = null, bool destroyWithScene = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | spawnPayload | The writer containing the spawn payload |
| bool | destroyWithScene | Should the object be destroyd when the scene is changed |
SpawnAsPlayerObject(ulong, Stream, bool)
Spawns an object across the network and makes it the player object for the given client
Declaration
public void SpawnAsPlayerObject(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | clientId | The clientId whos player object this is |
| Stream | spawnPayload | The writer containing the spawn payload |
| bool | destroyWithScene | Should the object be destroyd when the scene is changed |
SpawnWithOwnership(ulong, Stream, bool)
Spawns an object across the network with a given owner. Can only be called from server
Declaration
public void SpawnWithOwnership(ulong clientId, Stream spawnPayload = null, bool destroyWithScene = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | clientId | The clientId to own the object |
| Stream | spawnPayload | The writer containing the spawn payload |
| bool | destroyWithScene | Should the object be destroyd when the scene is changed |