Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Android game crashes after calling serverRpc calls

Discussion in 'Multiplayer' started by sidwak321, Aug 2, 2023.

  1. sidwak321

    sidwak321

    Joined:
    Mar 23, 2020
    Posts:
    1
    I made a simple android game for learning multiplayer and while testing it on my android device, whenever the android device calls serverRpc or receives clientRpc calls from my computer(i.e. server) it freezes for 2-3 seconds and then crashes. The android device is connected through wifi and computer through lan.

    The multiplayer works fine without any errors when testing on the computer using parrelSync. Tried backtracking the crash using logcat and shows errors at some "mathematics_1.cpp", but no idea what is the error. Also when the android device makes serverRpc calls, computer receives it properly (and also the computer sends the clientRpc calls) but the game crashes.

    I have attached the code for the main player script which handles all the networking. Any ideas what is going wrong here?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class MainPlayerScript : NetworkBehaviour
    7. {
    8.     private NetworkVariable<float> svMoved = new NetworkVariable<float>(writePerm: NetworkVariableWritePermission.Owner);
    9.     private NetworkVariable<float> ctMoved = new NetworkVariable<float>(writePerm: NetworkVariableWritePermission.Owner);
    10.  
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     public override void OnNetworkSpawn()
    17.     {
    18.         gameObject.name = "Player" + ((int)OwnerClientId).ToString();
    19.         if (IsOwner)
    20.         {
    21.             CanvasScript.instance.mainPlayerScript = this;
    22.         }
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.        
    28.     }
    29.  
    30.     public void MoverServerSp()
    31.     {
    32.         if (IsOwner)
    33.         {
    34.             svMoved.Value += 0.25f;
    35.             GameManagerScript.instance.totalSvMoved = svMoved.Value;
    36.             CanvasScript.instance.UpdateServerTx(GameManagerScript.instance.totalSvMoved);
    37.         }
    38.         RequestSvMoveServerRpc(0.25f);
    39.     }
    40.  
    41.     public void MoverClientSp()
    42.     {
    43.         if (IsOwner)
    44.         {
    45.             ctMoved.Value += 0.35f;
    46.             GameManagerScript.instance.totalCtMoved = ctMoved.Value;
    47.             CanvasScript.instance.UpdateClientTx(GameManagerScript.instance.totalCtMoved);
    48.         }
    49.         RequestCtMoveServerRpc(0.35f);
    50.     }
    51.  
    52.     [ServerRpc]
    53.     public void RequestSvMoveServerRpc(float amt)
    54.     {
    55.         FireSvMoveClientRpc(amt);
    56.     }
    57.  
    58.     [ClientRpc]
    59.     public void FireSvMoveClientRpc(float amt)
    60.     {
    61.         GameManagerScript.instance.MoveServerSp(amt);  
    62.         if (!IsOwner)
    63.         {
    64.             GameManagerScript.instance.totalSvMoved = svMoved.Value;
    65.             CanvasScript.instance.UpdateServerTx(GameManagerScript.instance.totalSvMoved);
    66.         }
    67.     }
    68.  
    69.     [ServerRpc]
    70.     public void RequestCtMoveServerRpc(float amt)
    71.     {
    72.         FireCtMoveClientRpc(amt);
    73.     }
    74.  
    75.     [ClientRpc]
    76.     public void FireCtMoveClientRpc(float amt)
    77.     {
    78.         GameManagerScript.instance.MoveClientSp(amt);
    79.         if (!IsOwner)
    80.         {
    81.             GameManagerScript.instance.totalCtMoved = ctMoved.Value;
    82.             CanvasScript.instance.UpdateClientTx(GameManagerScript.instance.totalCtMoved);
    83.         }
    84.     }
    85. }
    86.