docs.unity3d.com
    Show / Hide Table of Contents

    Class NetworkObject

    A component used to identify that a GameObject in the network

    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkObject
    Inherited Members
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(String, Single)
    MonoBehaviour.InvokeRepeating(String, Single, Single)
    MonoBehaviour.CancelInvoke(String)
    MonoBehaviour.IsInvoking(String)
    MonoBehaviour.StartCoroutine(String)
    MonoBehaviour.StartCoroutine(String, Object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(String)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(Object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.TryGetComponent(Type, Component)
    Component.TryGetComponent<T>(T)
    Component.GetComponent(String)
    Component.GetComponentInChildren(Type, Boolean)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(Boolean)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, Boolean)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(Boolean)
    Component.GetComponentsInChildren<T>(Boolean, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, Boolean)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(Boolean)
    Component.GetComponentsInParent<T>(Boolean, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(String)
    Component.SendMessageUpwards(String, Object, SendMessageOptions)
    Component.SendMessageUpwards(String, Object)
    Component.SendMessageUpwards(String)
    Component.SendMessageUpwards(String, SendMessageOptions)
    Component.SendMessage(String, Object)
    Component.SendMessage(String)
    Component.SendMessage(String, Object, SendMessageOptions)
    Component.SendMessage(String, SendMessageOptions)
    Component.BroadcastMessage(String, Object, SendMessageOptions)
    Component.BroadcastMessage(String, Object)
    Component.BroadcastMessage(String)
    Component.BroadcastMessage(String, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Component.rigidbody
    Component.rigidbody2D
    Component.camera
    Component.light
    Component.animation
    Component.constantForce
    Component.renderer
    Component.audio
    Component.networkView
    Component.collider
    Component.collider2D
    Component.hingeJoint
    Component.particleSystem
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.FindObjectsOfType(Type, Boolean)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    UnityEngine.Object.FindObjectsOfType<T>(System.Boolean)
    Object.FindObjectOfType<T>()
    UnityEngine.Object.FindObjectOfType<T>(System.Boolean)
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.FindObjectOfType(Type, Boolean)
    Object.ToString()
    Object.name
    Object.hideFlags
    Namespace: Unity.Netcode
    Syntax
    [AddComponentMenu("Netcode/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
    Boolean

    AutoObjectParentSync

    Whether or not to enable automatic NetworkObject parent synchronization.

    Declaration
    public bool AutoObjectParentSync
    Field Value
    Type Description
    Boolean

    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
    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
    Boolean

    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
    NetworkObject.SpawnDelegate

    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
    Boolean

    IsLocalPlayer

    Gets if the object is the personal clients player object

    Declaration
    public bool IsLocalPlayer { get; }
    Property Value
    Type Description
    Boolean

    IsOwnedByServer

    Gets Whether or not the object is owned by anyone

    Declaration
    public bool IsOwnedByServer { get; }
    Property Value
    Type Description
    Boolean

    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
    Boolean

    IsPlayerObject

    Gets if this object is a player object

    Declaration
    public bool IsPlayerObject { get; }
    Property Value
    Type Description
    Boolean

    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
    Nullable<Boolean>

    IsSpawned

    Gets if the object has yet been spawned across the network

    Declaration
    public bool IsSpawned { get; }
    Property Value
    Type Description
    Boolean

    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
    UInt64

    OwnerClientId

    Gets the ClientId of the owner of this NetworkObject

    Declaration
    public ulong OwnerClientId { get; }
    Property Value
    Type Description
    UInt64

    Methods

    ChangeOwnership(UInt64)

    Changes the owner of the object. Can only be called from server

    Declaration
    public void ChangeOwnership(ulong newOwnerClientId)
    Parameters
    Type Name Description
    UInt64 newOwnerClientId

    The new owner clientId

    Despawn(Boolean)

    Despawns the GameObject of this NetworkObject and sends a destroy message for it to all connected clients.

    Declaration
    public void Despawn(bool destroy = true)
    Parameters
    Type Name Description
    Boolean destroy

    (true) the GameObject will be destroyed (false) the GameObject will persist after being despawned

    GetObservers()

    Returns Observers enumerator

    Declaration
    public HashSet<ulong>.Enumerator GetObservers()
    Returns
    Type Description
    HashSet.Enumerator<>

    Observers enumerator

    IsNetworkVisibleTo(UInt64)

    Whether or not this object is visible to a specific client

    Declaration
    public bool IsNetworkVisibleTo(ulong clientId)
    Parameters
    Type Name Description
    UInt64 clientId

    The clientId of the client

    Returns
    Type Description
    Boolean

    True if the client knows about the object

    NetworkHide(List<NetworkObject>, UInt64)

    Hides a list of NetworkObjects from the targeted client.

    Declaration
    public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientId)
    Parameters
    Type Name Description
    List<NetworkObject> networkObjects

    The NetworkObjects that will become "netcode invisible" to the targeted client

    UInt64 clientId

    The targeted client

    Remarks

    Usage: Use to stop sending updates to the targeted client, "netcode invisible", for the currently visible NetworkObjects.

    Dynamically Spawned: NetworkObjects will be despawned and destroyed on the targeted client's side.
    In-Scene Placed: NetworkObjects will only be despawned on the targeted client's side.

    See Also:
    NetworkHide(UInt64)
    NetworkShow(UInt64) or NetworkShow(List<NetworkObject>, UInt64)

    NetworkHide(UInt64)

    Hides the NetworkObject from the targeted client.

    Declaration
    public void NetworkHide(ulong clientId)
    Parameters
    Type Name Description
    UInt64 clientId

    The targeted client

    Remarks

    Usage: Use to stop sending updates to the targeted client, "netcode invisible", for a currently visible NetworkObject.

    Dynamically Spawned: NetworkObjects will be despawned and destroyed on the targeted client's side.
    In-Scene Placed: NetworkObjects will only be despawned on the targeted client's side.

    See Also:
    NetworkHide(List<NetworkObject>, UInt64)
    NetworkShow(UInt64) or NetworkShow(List<NetworkObject>, UInt64)

    NetworkShow(List<NetworkObject>, UInt64)

    Makes a list of previously hidden NetworkObjects "netcode visible" for the client specified.

    Declaration
    public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientId)
    Parameters
    Type Name Description
    List<NetworkObject> networkObjects

    The objects to become "netcode visible" to the targeted client

    UInt64 clientId

    The targeted client

    Remarks

    Usage: Use to start sending updates for previously hidden NetworkObjects to the targeted client.

    Dynamically Spawned: NetworkObjects will be instantiated and spawned on the targeted client's side.
    In-Scene Placed: Already instantiated but despawned NetworkObjects will be spawned on the targeted client's side.

    See Also:
    NetworkShow(UInt64)
    NetworkHide(UInt64) or NetworkHide(List<NetworkObject>, UInt64)

    NetworkShow(UInt64)

    Makes the previously hidden NetworkObject "netcode visible" to the targeted client.

    Declaration
    public void NetworkShow(ulong clientId)
    Parameters
    Type Name Description
    UInt64 clientId

    The targeted client

    Remarks

    Usage: Use to start sending updates for a previously hidden NetworkObject to the targeted client.

    Dynamically Spawned: NetworkObjects will be instantiated and spawned on the targeted client side.
    In-Scene Placed: The instantiated but despawned NetworkObjects will be spawned on the targeted client side.

    See Also:
    NetworkShow(UInt64)
    NetworkHide(UInt64) or NetworkHide(List<NetworkObject>, UInt64)

    RemoveOwnership()

    Removes all ownership of an object from any client. Can only be called from server

    Declaration
    public void RemoveOwnership()

    Spawn(Boolean)

    Spawns this NetworkObject across the network. Can only be called from the Server

    Declaration
    public void Spawn(bool destroyWithScene = false)
    Parameters
    Type Name Description
    Boolean destroyWithScene

    Should the object be destroyed when the scene is changed

    SpawnAsPlayerObject(UInt64, Boolean)

    Spawns a NetworkObject across the network and makes it the player object for the given client

    Declaration
    public void SpawnAsPlayerObject(ulong clientId, bool destroyWithScene = false)
    Parameters
    Type Name Description
    UInt64 clientId

    The clientId who's player object this is

    Boolean destroyWithScene

    Should the object be destroyed when the scene is changed

    SpawnWithOwnership(UInt64, Boolean)

    Spawns a NetworkObject across the network with a given owner. Can only be called from server

    Declaration
    public void SpawnWithOwnership(ulong clientId, bool destroyWithScene = false)
    Parameters
    Type Name Description
    UInt64 clientId

    The clientId to own the object

    Boolean destroyWithScene

    Should the object be destroyed when the scene is changed

    TrySetParent(NetworkObject, Boolean)

    Set the parent of the NetworkObject transform.

    Declaration
    public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
    Parameters
    Type Name Description
    NetworkObject parent

    The new parent for this NetworkObject transform will be the child of.

    Boolean 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
    Boolean

    Whether or not reparenting was successful.

    TrySetParent(GameObject, Boolean)

    Set the parent of the NetworkObject transform.

    Declaration
    public bool TrySetParent(GameObject parent, bool worldPositionStays = true)
    Parameters
    Type Name Description
    GameObject parent

    The new parent for this NetworkObject transform will be the child of.

    Boolean 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
    Boolean

    Whether or not reparenting was successful.

    TrySetParent(Transform, Boolean)

    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.

    Boolean 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
    Boolean

    Whether or not reparenting was successful.

    In This Article
    • Fields
      • AlwaysReplicateAsRoot
      • AutoObjectParentSync
      • CheckObjectVisibility
      • DontDestroyWithOwner
      • IncludeTransformWhenSpawning
    • Properties
      • DestroyWithScene
      • IsLocalPlayer
      • IsOwnedByServer
      • IsOwner
      • IsPlayerObject
      • IsSceneObject
      • IsSpawned
      • NetworkManager
      • NetworkObjectId
      • OwnerClientId
    • Methods
      • ChangeOwnership(UInt64)
      • Despawn(Boolean)
      • GetObservers()
      • IsNetworkVisibleTo(UInt64)
      • NetworkHide(List<NetworkObject>, UInt64)
      • NetworkHide(UInt64)
      • NetworkShow(List<NetworkObject>, UInt64)
      • NetworkShow(UInt64)
      • RemoveOwnership()
      • Spawn(Boolean)
      • SpawnAsPlayerObject(UInt64, Boolean)
      • SpawnWithOwnership(UInt64, Boolean)
      • TrySetParent(NetworkObject, Boolean)
      • TrySetParent(GameObject, Boolean)
      • TrySetParent(Transform, Boolean)
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023