Search Unity

Question What are the requirements for changes to be synced?

Discussion in 'Netcode for GameObjects' started by kemijo, Oct 29, 2022.

  1. kemijo

    kemijo

    Joined:
    Apr 7, 2014
    Posts:
    18
    Apologies for the newbie question, but I am having trouble finding a clear answer and not sure I am doing something wrong.

    If I stick this in a NetworkBehaviour...

    Code (CSharp):
    1. void Update()
    2. {
    3.     if(!IsOwner) return;
    4.        
    5.     if( Input.GetKeyDown(KeyCode.T) )
    6.     {
    7.         TestServerRpc();
    8.     }  
    9. }
    10.  
    11. [ServerRpc]
    12. void TestServerRpc()
    13. {
    14.     Debug.Log("Running ServerRpc on Client: " + OwnerClientId);
    15. }
    ...and attach it to the network spawned Player Prefab object, it runs fine on the Host and Client.
    But if I attach the same Network Behaviour to an object that already exists in the scene, is not spawned but was added to the Network Prefabs list, it doesn't sync (omitting the IsOwner check throws an error).

    What are the requirements for network sync? Is it possible to have synced behaviour on non-spawned Network objects that live in the scene (with NetworkObject/NetworkBehaviour components, and added to the Network prefabs list on the NetworkManager).
     
  2. kemijo

    kemijo

    Joined:
    Apr 7, 2014
    Posts:
    18
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,956