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 NetworkObjectReference in RPC is Null

Discussion in 'Netcode for GameObjects' started by Djoxon, Aug 22, 2023.

  1. Djoxon

    Djoxon

    Joined:
    Apr 22, 2023
    Posts:
    4
    I have this ClientRpc that the server is calling:

    Code (CSharp):
    1.     [ClientRpc]
    2.     private void CanvasClientRpc(NetworkObjectReference CanvasSent)
    3.     {
    4.  
    5.         NetworkObject Canvas = CanvasSent;
    6.         if (Canvas == null)
    7.         {
    8.             Debug.Log("Canvas is null");
    9.         }
    10.     }
    Before I call this function on the server, I check the value of the NetworkObject that is passed to this function and it is not null, but in the CanvasClientRpc method Canvas is null, "Canvas is null" is logged.

    In documentation it says:
    The implicit conversion to NetworkObject / GameObject will result in Null if the reference can't be found.
    https://docs-multiplayer.unity3d.co...kobject-serialization/#networkobjectreference

    Why is it null? Has anyone had a simillar issue or any ideas how I can fix this?
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I think you need to do something like this:
    networkObjectReference.TryGet(out NetworkObject obj)
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,550
    Why would you send a Canvas object? That spells trouble! The canvas could be totally different on a client due to the client using a different resolution, aspect ratio and so on.

    Generally speaking you should NEVER synchronize the UI over the network. Synchronize the game state instead. For example, in a card game you don‘t synchronize the card‘s UIImage as it gets played on the table. Instead a simple RPC „play card xyz“ is sent to clients and each client then locally animates the card flying and flipping onto the table.
     
    Djoxon likes this.
  4. Djoxon

    Djoxon

    Joined:
    Apr 22, 2023
    Posts:
    4
    That is exactly what i did and now everything is working fine. Thank you for the suggestion!