Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question ServerRPC Not Being Called

Discussion in 'Netcode for GameObjects' started by zMayTricks, Oct 30, 2021.

  1. zMayTricks

    zMayTricks

    Joined:
    Sep 26, 2019
    Posts:
    4
    I'm trying to spawn the player after a button is clicked, but when it's clicked the ServerRPC seems to get ignored and doesn't even attempt to do what's inside. When looking in the console, "SpawnPlayer called" and "SpawnPlayer finished" is displayed, but the other logs are not.

    Code (CSharp):
    1. public class GameManager : NetworkBehaviour
    2. {
    3.     [SerializeField]
    4.     private GameObject playerPrefab;
    5.  
    6.     // Start is called before the first frame update
    7.     void Awake()
    8.     {
    9.        
    10.     }
    11.  
    12.     public void SpawnPlayer(ulong clientId)
    13.     {
    14.         if (IsOwner)
    15.         {
    16.             Debug.Log("SpawnPlayer called");
    17.             SpawnPlayerServerRpc(clientId);
    18.             Debug.Log("SpawnPlayer finished");
    19.         }
    20.     }
    21.  
    22.     [ServerRpc(RequireOwnership =false)]
    23.     private void SpawnPlayerServerRpc(ulong clientId)
    24.     {
    25.         Debug.Log("Client wants to spawn player");
    26.         GameObject go = Instantiate(playerPrefab);
    27.         go.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
    28.         ulong playerId = go.GetComponent<NetworkObject>().NetworkObjectId;
    29.  
    30.         SpawnPlayerClientRpc(playerId);
    31.     }
    32.  
    33.     [ClientRpc]
    34.     private void SpawnPlayerClientRpc(ulong playerId)
    35.     {
    36.         Debug.Log("Spawned Player");
    37.         NetworkObject player = NetworkSpawnManager.SpawnedObjects[playerId];
    38.     }
    39. }
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    When is `SpawnPlayer` getting called? Awake/Start maybe? Is this NetworkBehaviour on the player object?
     
  3. zMayTricks

    zMayTricks

    Joined:
    Sep 26, 2019
    Posts:
    4
    I have a Panel with a separate script and button that has an on click function, which calls SpawnPlayer. The reason I separated the click function into another script is because someone said they had issues getting the client ID instead of the host ID when assigning an ID to the player object. That separate script is MonoBehaviour, but changing it to NetworkBehaviour didn't fix it. Here's that script in case it helps:
    Code (CSharp):
    1. public class SpawnPlayer : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private GameObject spawnPlayerPanel;
    5.     [SerializeField]
    6.     private GameManager gm;
    7.  
    8.     // Start is called before the first frame update
    9.     void Awake()
    10.     {
    11.         spawnPlayerPanel = GameObject.Find("SpawnPlayerPanel");
    12.         gm = FindObjectOfType<GameManager>();
    13.     }
    14.  
    15.     public void OnPlayClick()
    16.     {
    17.         gm.SpawnPlayer(NetworkManager.Singleton.LocalClientId);
    18.         //spawnPlayerPanel.SetActive(false);
    19.     }
    20. }
    As for the player object, this script is on a GameManager object and not the player object. I'm trying to instantiate the player object using the prefab, and have the Network Manager spawn it as a player using the ServerRPC, but it's not executing.
     
  4. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    Okay, so the separate script looks like and it doesn't have to be a NetworkBehaviour.

    Regarding your GameManager make sure that:
    - The GameManager NetworkBehaviour is on a scene GameObject which also has a NetworkObject component.
    - You run `NetworkManager.StartClient();` first before clicking the button.
     
  5. zMayTricks

    zMayTricks

    Joined:
    Sep 26, 2019
    Posts:
    4
    I have a NetworkObject component on my GameManager. And is running StartClient() necessary if I already called StartHost()? I've been hosting the game without clients for now.
     
  6. SojoTaku

    SojoTaku

    Joined:
    Dec 14, 2021
    Posts:
    1
    Ayo zMayTricks did you find a solution? I have also encountered a similar problem
     
  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    105
    Hey @SojoTaku did you solve the issue in the end? If you didn't, maybe the answers here will be helpful to solve it. If they are not, could you please post your scene's setup and scripts so I can have a look at them and help you?