Search Unity

Bug Netcode: ServerRpc never getting called

Discussion in 'Netcode for GameObjects' started by Xa456789, Mar 25, 2023.

  1. Xa456789

    Xa456789

    Joined:
    Aug 5, 2022
    Posts:
    24
    So I have been making a multiplayer game and I have been getting so Issues:
    Code (csharp):
    1. public void PlayerWantsToSkipTurn()
    2.     {
    3.         if (GetOwnership() && IsClient)
    4.         {
    5.             Debug.Log("Client " + IsClient + "; Server " + IsServer + "; Host " + IsHost);
    6.             SetSkipStateServerRpc();
    7.  
    8.             //if (GetWantsToSkipState() && !unitsThatWantToEndTurnList.Contains(this))
    9.             //{
    10.             //    foreach (Unit unit in LevelGrid.Instance.GetAllUnitList())
    11.             //    {
    12.             //        unit.AddWantsToSkipList(this);
    13.             //    }
    14.             //}
    15.  
    16.             //if (!GetWantsToSkipState() && unitsThatWantToEndTurnList.Contains(this))
    17.             //{
    18.             //    foreach (Unit unit in LevelGrid.Instance.GetAllUnitList())
    19.             //    {
    20.             //        unit.RemoveWantsToSkipList(this);
    21.             //    }
    22.             //}
    23.  
    24.             //while (beforechangeWantsToSkipState == GetWantsToSkipState())
    25.             //{
    26.              
    27.             //}
    28.  
    29.             foreach(Unit teamTurnUnits in LevelGrid.Instance.GetTeamTurnUnits())
    30.             {
    31.                 if (teamTurnUnits.GetWantsToSkipState())
    32.                     AddWantsToSkipList(teamTurnUnits);
    33.                 if(!teamTurnUnits.GetWantsToSkipState())
    34.                     RemoveWantsToSkipList(teamTurnUnits);
    35.                 Debug.Log(teamTurnUnits.GetWantsToSkipState() + "|Actually:" + GetWantsToSkipState());
    36.             }
    37.  
    38.             Debug.Log(unitsThatWantToEndTurnList.Count);
    39.             CheckEndTurnUnits();
    40.         }
    41.     }
    The Rpc isn´t even getting called anymore, what is happening?
    On the Host it works, but not on the Client and I checked with Debug.Log() .
    Its even worse, somehow there is no error like that you can´t call an ServerRpc as a Server, no errors but it just never gets to run why is that?
    This is the ServerRpc:
    Code (csharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.     private void SetSkipStateServerRpc()
    3.     {
    4.         Debug.Log("1. " + wantsToSkip.Value);
    5.         wantsToSkip.Value = !wantsToSkip.Value;
    6.         Debug.Log("2. " + wantsToSkip.Value);
    7.     }
    I have also been getting Issues with a mine placement mechanic:
    Code (csharp):
    1. public override void TakeAction(GridPos gridPos, Action onActionComplete)
    2.     {
    3.         if (!IsClient) return;
    4.  
    5.         ActionStart(onActionComplete);
    6.         this.placePos = LevelGrid.Instance.GetWorldPos(gridPos);
    7.         clonedObj = Instantiate(placeObj, placePos, Quaternion.identity);
    8.         //PlaceMineServerRpc();
    9.         teamTurnId = GetComponent<Unit>().GetCurrentTeamId();
    10.         PlaceMineServerRpc();
    11.         //HideMineClientRpc();
    12.     }
    So kind of the same thing, on the host all workes fine but not on the Client, as I wrote it I made a ServerRpc so that both, the Host and the Client can place them on the Network. But everytime I run it, it says something like: NullReferencExeption for the clonedObject but it is saved into a Variable:
    Code (csharp):
    1. private GameObject clonedObj;
    The ServerRpc uses:
    Code (csharp):
    1. clonedObj.GetComponent<NetworkObject>().Spawn(true);
    Probably I did something wrong, with the Code, maybe its all some error outside the code and more like configuration error. So if anybody can help me, I would appreciate it.
    Details:
    I am using Unity 2021.3.16f, I know that there is a newer version but I didn´t bothered updating.
    I think I am using the newest version of Netcode: 1.1.0
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    The newest version of netcode is 1.2.0. Try to run the client from the computer and see if the network objec is actually spawned. If it isn't, you should find a way to spawn it
     
  3. Xa456789

    Xa456789

    Joined:
    Aug 5, 2022
    Posts:
    24
    But I don‘t see any newer Versions
     
  4. Xa456789

    Xa456789

    Joined:
    Aug 5, 2022
    Posts:
    24
    Found and downloaded it.
    Already seeing improvements: when I write if(!IsClient) return; and debug.log after that it doesn‘t say IsServer = true, IsClient = true, IsHost = true, it only debug.logs: IsClient = true
     
    Last edited: Mar 25, 2023
  5. Xa456789

    Xa456789

    Joined:
    Aug 5, 2022
    Posts:
    24
    So after I installed the new version I had high hopes, but the ServerRpc still doesn‘t get called
     
  6. MohammadAlizadeh

    MohammadAlizadeh

    Joined:
    Apr 16, 2015
    Posts:
    26
    Yup ServerRpc never gets called when running as server, netcode broken
     
  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    @Xa456789 ServerRPC are executed on the server when called by clients. When you say "On the Host it works, but not on the Client" what do you mean?

    Where are you calling the RPC from?