Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Resolved How to let the client move the server`s objects?

Discussion in 'Netcode for GameObjects' started by superdracon1, Mar 3, 2024.

  1. superdracon1

    superdracon1

    Joined:
    Oct 16, 2023
    Posts:
    3
    There is an object in a scene called P1 with a "Network object" and "Network Transform" components on it.

    using Network Manager I spawn a prefab of an object that has this script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class ControllP1 : NetworkBehaviour
    7. {
    8.  
    9.     [SerializeField] private Vector3 StartPosition;
    10.     [SerializeField] private GameObject P1;
    11.  
    12.     void Start()
    13.     {
    14.         P1 = GameObject.Find("P1");
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.  
    20.         if (Input.GetKeyDown(KeyCode.R))
    21.         {
    22.             RestartCharacterPositionRpc();
    23.         }
    24.  
    25.     }
    26.  
    27.     [Rpc(SendTo.Server)]
    28.     void RestartCharacterPositionRpc()
    29.     {
    30.  
    31.         P1.transform.position = StartPosition;
    32.  
    33.     }
    34.  
    35. }

    When the host presses the button P1 object position is set to StartPosition. However, when the client presses the button, nothing happens.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,018
    When you spawn it you can‘t use Find in Start. Find by string is brittle anyway, it‘s likely to be called „P1(Clone)“ after instantiating. And in Start it may not have been spawned yet.

    When you spawn it, either the P1 object tells other local scripts that it was spawned, or the server sends an Rpc to clients with the spawned object‘s NetworkObjectId so that they can look it up in NetworkManager.Singleton.SpawnManager.SpawnedObjects[id] (from the top off my head, names may be similar)
     
  3. superdracon1

    superdracon1

    Joined:
    Oct 16, 2023
    Posts:
    3
    I played a little bit with the project today and understood the problem. The client could not send Rpc to the server! And it could not do it, because it did not connect to the server (I saw it when I enabled the devlogs in the network manager). I backuped the project, remade the scene setup and scripts and now it is working well, even tho it is almost identical to the original project.

    I do not know why I initially got the problem. Maybe because I updated the "netcode for gameobjects" package in the middle of doing the project, maybe it is somehow correlated to this thread https://forum.unity.com/threads/cli...client-build-to-editor-host-does-not.1546613/.
     
    Last edited: Mar 4, 2024