Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Server Rpc not getting called

Discussion in 'Netcode for GameObjects' started by MacoPuco, Sep 6, 2023.

  1. MacoPuco

    MacoPuco

    Joined:
    Feb 21, 2022
    Posts:
    3
    Hi, I have a problem that when I call a function with ServerRpc, it's not doing anything. No error messages are visible in the console. Here is the code:

    Code (CSharp):
    1.     void OnMouseDown()
    2.     {
    3.         Debug.Log("Clicked");
    4.         SpawnPieceServerRpc();
    5.     }
    6.  
    7.     [ServerRpc(RequireOwnership = false)]
    8.     private void SpawnPieceServerRpc()
    9.     {
    10.         Debug.Log("Spawned");
    11.         GameObject pieceGO = (GameObject)Instantiate(pieceToSpawn, transform.position, Quaternion.identity);
    12.         pieceGO.GetComponent<NetworkObject>().Spawn();
    13.     }
    This script has NetworkBehaviour, a Network object is also attached to the object. The object that I'm trying to spawn also has the network object and is in the network prefabs in network manager.

    So every time I click on this object, another should spawn in it's place, but nothing happens. Console only writes "Clicked" but not "Spawned". Any ideas?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,577
    Hmmm I can only think of the client not being connected respectively the host hasn't run the StartHost() method yet. Or the script that you're running isn't actually on a NetworkObject - it may be on one but perhaps the script that's actually running is also somewhere else, perhaps on the GUI?

    If it's set up as you describe it should work but maybe double-check and verify that the Console doesn't have the display of warning/error messages disabled.
     
  3. MacoPuco

    MacoPuco

    Joined:
    Feb 21, 2022
    Posts:
    3
    Hi, thank you for your reply.
    I checked everything you said, but everything should be ok. Although it might be worth mentioning that the game object with this script is getting instantiated by another object. I'm am not spawning the object because it should only be visible to the owner.

    After further testing I figured out that when I spawn this object in before I create the host, everything is working (not when I create it, then connect through client, this way it still doesnt work). I need this to be also be working when the player is already connected (to instantiate this object which is visible only to the player that spawned it and when he clicks on it, an object spawns that is visible to all the other players).
     
  4. MacoPuco

    MacoPuco

    Joined:
    Feb 21, 2022
    Posts:
    3
    Hi,
    turns out spawning this object right before running the ServerRpc works like I wanted it to. I guess only spawned network objects can access ServerRpc.