ServerRPC Not Being Called

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.

public class GameManager : NetworkBehaviour
{
    [SerializeField]
    private GameObject playerPrefab;

    // Start is called before the first frame update
    void Awake()
    {
       
    }

    public void SpawnPlayer(ulong clientId)
    {
        if (IsOwner)
        {
            Debug.Log("SpawnPlayer called");
            SpawnPlayerServerRpc(clientId);
            Debug.Log("SpawnPlayer finished");
        }
    }

    [ServerRpc(RequireOwnership =false)]
    private void SpawnPlayerServerRpc(ulong clientId)
    {
        Debug.Log("Client wants to spawn player");
        GameObject go = Instantiate(playerPrefab);
        go.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
        ulong playerId = go.GetComponent<NetworkObject>().NetworkObjectId;

        SpawnPlayerClientRpc(playerId);
    }

    [ClientRpc]
    private void SpawnPlayerClientRpc(ulong playerId)
    {
        Debug.Log("Spawned Player");
        NetworkObject player = NetworkSpawnManager.SpawnedObjects[playerId];
    }
}

When is SpawnPlayer getting called? Awake/Start maybe? Is this NetworkBehaviour on the player object?

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:

public class SpawnPlayer : MonoBehaviour
{
    [SerializeField]
    private GameObject spawnPlayerPanel;
    [SerializeField]
    private GameManager gm;

    // Start is called before the first frame update
    void Awake()
    {
        spawnPlayerPanel = GameObject.Find("SpawnPlayerPanel");
        gm = FindObjectOfType<GameManager>();
    }

    public void OnPlayClick()
    {
        gm.SpawnPlayer(NetworkManager.Singleton.LocalClientId);
        //spawnPlayerPanel.SetActive(false);
    }
}

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.

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.

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.

Ayo zMayTricks did you find a solution? I have also encountered a similar problem

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?