Search Unity

ServerRPC Getting called by the client

Discussion in 'Multiplayer' started by xarfralm, Apr 18, 2021.

  1. xarfralm

    xarfralm

    Joined:
    Jul 5, 2013
    Posts:
    5
    Hello,

    After a while developing using MLAPI, my client started to stop sendding ServerRPC to the server and the function get called directly on the client.
    I don't think their is anything wrong with my code because even this simple test doesn't work :
    I have also to tell that everythings worked fine yesterday...

    Code (CSharp):
    1. using MLAPI.Messaging;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NetworkTest : MLAPI.NetworkBehaviour
    7. {
    8.     public bool test;
    9.  
    10.     public void Update()
    11.     {
    12.         if (IsClient && test)
    13.         {
    14.             Test();
    15.         }
    16.     }
    17.  
    18.     public void Test()
    19.     {
    20.         Debug.Log("Launch on client");
    21.         ClickServerRpc();
    22.     }
    23.  
    24.     [ServerRpc]
    25.     public void ClickServerRpc()
    26.     {
    27.         Debug.Log("Launch on server");
    28.     }
    29. }
    30.  


    Here, the client show the 2 debugs and the server show nothing.
    I have tried to recompile, removing some of my networkbehaviour, reimport assets and reimport MLAPI with no success.
    If I copy this code on another project that work

    If one of you have an idea of what going on that will be cool !

    Thanks in advance for your help.
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    This is a bug right now which we have in MLAPI. Try deleting Library, Temp and obj folder and the sln and csproj files of your project that can fix it.
     
  3. xarfralm

    xarfralm

    Joined:
    Jul 5, 2013
    Posts:
    5
    I tried it with no success. I imported on a new project my scripts one by one and figured out that this script cause the problem but I don't understand why. If I remove it everything work fine even if this script is not placed anywhere...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using MLAPI;
    6. using MLAPI.Messaging;
    7.  
    8. [RequireComponent(typeof(Rigidbody))]
    9. [RequireComponent(typeof(Animator))]
    10. public class Dice : NetworkBehaviour
    11. {
    12.     enum DiceState
    13.     {
    14.         WAIT,
    15.         DRAGGED,
    16.         LAUNCHED,
    17.         STABLE
    18.     }
    19.  
    20.     DiceState state = DiceState.WAIT;
    21.     Rigidbody rigidbody;
    22.  
    23.     public float force = 2.0f;
    24.     public float angularForce = 2.0f;
    25.     public LayerMask mask;
    26.     public float secondAfterDisolve = 2F;
    27.     public Transform valuesTransform;
    28.     public Transform arrowTransform;
    29.     public MeshRenderer arrowMesh;
    30.     public float minDistance;
    31.     public float maxDistance;
    32.  
    33.     public Gradient arrowColor;
    34.     public Color invalidColor;
    35.  
    36.     private float timeAtLaunch = 0F;
    37.     private bool isOnGoodPosition = false;
    38.     private float desiredForce;
    39.     private void Start()
    40.     {
    41.         rigidbody = GetComponent<Rigidbody>();
    42.         rigidbody.isKinematic = true;
    43.         GetComponent<Animator>().SetTrigger("Start");
    44.         transform.localEulerAngles = Vector3.zero;
    45.         arrowTransform.gameObject.SetActive(false);
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void Update()
    50.     {
    51.         switch (state)
    52.         {
    53.             case DiceState.WAIT:
    54.                 break;
    55.             case DiceState.DRAGGED:
    56.                 Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
    57.                 RaycastHit hit;
    58.  
    59.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
    60.                 {
    61.                     Vector3 mouseTarget = hit.point;
    62.                     mouseTarget.y = transform.position.y;
    63.                     arrowTransform.LookAt(mouseTarget);
    64.                     float distance = (transform.position - mouseTarget).magnitude;
    65.                     if (distance < minDistance)
    66.                     {
    67.                         arrowMesh.material.SetColor("Color_Emission", invalidColor);
    68.                     }
    69.                     else
    70.                     {
    71.                         if (Physics.Raycast(transform.position, arrowTransform.forward, out hit, minDistance, mask))
    72.                         {
    73.                             arrowMesh.material.SetColor("Color_Emission", invalidColor);
    74.                         }
    75.                         else
    76.                         {
    77.                             desiredForce = Mathf.Min((distance - minDistance), maxDistance) / (maxDistance - minDistance);
    78.                             Vector3 newScale = arrowTransform.localScale;
    79.                             newScale.z = Mathf.Lerp(0.5f, 1.5f, desiredForce);
    80.                             arrowTransform.localScale = newScale;
    81.                             arrowMesh.material.SetColor("Color_Emission", arrowColor.Evaluate(desiredForce));
    82.                             isOnGoodPosition = true;
    83.                         }
    84.                     }
    85.                 }
    86.                 break;
    87.             case DiceState.LAUNCHED: // The dice is checked only on server and then resend on client
    88.                 if (IsServer)
    89.                 {
    90.                     if (rigidbody.velocity.sqrMagnitude < 0.01 && Time.realtimeSinceStartup - timeAtLaunch > 0.5F)
    91.                     {
    92.                         state = DiceState.STABLE;
    93.                         StartCoroutine(ValidateDice());
    94.                     }
    95.                 }
    96.                 break;
    97.             default:
    98.                 break;
    99.         }
    100.     }
    101.  
    102.     [ClientRpc]
    103.     private void ValidateDiceClientRpc(int diceValue, ulong clientId)
    104.     {
    105.         Debug.Log(clientId.ToString() + " : " + diceValue);
    106.         StartCoroutine(ValidateDiceClient());
    107.     }
    108.  
    109.     private IEnumerator ValidateDiceClient()
    110.     {
    111.         yield return new WaitForSeconds(secondAfterDisolve);
    112.         GetComponent<Animator>().SetTrigger("End");
    113.     }
    114.  
    115.     private IEnumerator ValidateDice()
    116.     {
    117.         yield return new WaitForSeconds(1F);
    118.         float lastY = -Mathf.Infinity;
    119.         Transform lastChild = null;
    120.         foreach (Transform item in valuesTransform)
    121.         {
    122.             if (lastChild == null || item.position.y > lastY)
    123.             {
    124.                 lastY = item.position.y;
    125.                 lastChild = item;
    126.             }
    127.         }
    128.         rigidbody.isKinematic = true;
    129.         int value = int.Parse(lastChild.gameObject.name);
    130.         ValidateDiceClientRpc(value, OwnerClientId);
    131.         MLAPI.Spawning.NetworkSpawnManager.GetPlayerNetworkObject(OwnerClientId).GetComponent<Network.NetworkPlayer>().isOnDice.Value--;
    132.         yield return new WaitForSeconds(secondAfterDisolve + 2F);
    133.         Destroy(this.gameObject);
    134.     }
    135.  
    136.     public void OnDiceClick(InputValue value)
    137.     {
    138.         if (!IsOwner) return;
    139.         if (state == DiceState.LAUNCHED || state == DiceState.STABLE) return;
    140.         if (value.isPressed)
    141.         {
    142.             if (IsLocalPlayer)
    143.             {
    144.                 arrowTransform.gameObject.SetActive(true);
    145.             }
    146.             state = DiceState.DRAGGED;
    147.         }
    148.         else if (state == DiceState.DRAGGED)
    149.         {
    150.             if (!isOnGoodPosition)
    151.             {
    152.                 state = DiceState.WAIT;
    153.                 return;
    154.             }
    155.             if (IsLocalPlayer)
    156.             {
    157.                 arrowTransform.gameObject.SetActive(false);
    158.             }
    159.             rigidbody.isKinematic = false;
    160.             state = DiceState.LAUNCHED;
    161.             LaunchDiceServerRpc(arrowTransform.forward * -force * desiredForce);
    162.         }
    163.     }
    164.    
    165.     [ServerRpc]
    166.     public void LaunchDiceServerRpc(Vector3 dir)
    167.     {
    168.         rigidbody.AddForce(dir, ForceMode.Impulse);
    169.         rigidbody.AddTorque(Random.rotation * transform.forward * angularForce);
    170.         timeAtLaunch = Time.realtimeSinceStartup;
    171.     }
    172.  
    173.     [ClientRpc]
    174.     public void RefusedLaunch()
    175.     {
    176.         if (IsOwner)
    177.         {
    178.             state = DiceState.WAIT;
    179.         }
    180.     }
    181. }
    182.  
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Just to confirm are you running the server in Server or Host mode?
     
  5. xarfralm

    xarfralm

    Joined:
    Jul 5, 2013
    Posts:
    5
    In server mode
     
    luke-unity likes this.
  6. xarfralm

    xarfralm

    Joined:
    Jul 5, 2013
    Posts:
    5
    I figured out what's going on. I missed the ClientRpc prefix on my function RefusedLaunch. I added it and know that work. But the last time I missed to add this prefix I got an error from MLAPI. It's strange that I didn't get the error here.
     
  7. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    I cannot reproduce this. If I copy your script (and remove the code inside functions to fix compile issues) I get the `ClientRpc method must end with 'ClientRpc' suffix!` error in the console for the RefusedLaunch function.
     
  8. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Just to confirm that you may not get these errors: I do not get any network errors for the same type of mistake of bad rpc naming, even if I have set the log to "Developer"...

    Bye,

    Jean
     
  9. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Just to make also a request that this kind of totally out of blue way to solved known bugs, should be in the section Troubleshooting of the doc... I lost a day on that, cursing at everything and everyone...

    Please maintain the troubleshooting page to your current knowledge of issues :)

    Bye,

    Jean
     
  10. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    now that I cleaned up the library folder, and Rpc are back to normal, I do get the error on malfformed rpc methods:

    ILPostProcessor - NetworkBehaviourILPP failed to run on Assembly-CSharp


    Bye,

    Jean