Search Unity

Question Keeping a reference to an instantiated and spawned object from a ServerRpc method

Discussion in 'Netcode for GameObjects' started by BurnEmDown, Sep 1, 2022.

  1. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    Hello, I've had an issue where I tried to instantiate and then spawn a prefab in a ServerRpc method which was called and passed from one script to another, but the prefab was sent as null (link: https://forum.unity.com/threads/serverrpc-called-twice.1330053/#post-8406318). The issue was solved, for now, by not passing the prefab as an argument to the ServerRpc method and instead keeping a reference to the prefab in the script which held the method. However, I need to have a reference to the object which was just spawned in the 1st script, and as much as I understand, ServerRpc methods can't return any value.

    Is there a way to do something like this?

    Code (CSharp):
    1. //HexGridChunk chunk = chunks[i++] = Instantiate(chunkPrefab); // non-network method
    2. SpawnerController.Instance.SpawnChunkServerRpc(); // new method which contains the instantiation as well
    3.  
    4. // now the HexGridChunk which was instantiated and spawned needs to be referenced, for example added to the chunks[] array
     
  2. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    I realized that I also forgot to mention, the reason why I moved the instantiation of the object to the ServerRpc method was because that if I tried to first instantiate it and then send the object as a parameter to the ServerRpc method to only have it spawned, for some reason the ServerRpc method gets called twice and in the first time the object arrives as null, then in the second time the object isn't null and is spawned correctly. When I try to instantiate new objects of the same type and spawning them, then they will be successfully spawned with only one call.

    If I try to add a null check to the ServerRpc method, it will not be called for the 2nd time and no objects will be spawned (but it will be instantiated on the client calling the method since that happens before the Rpc call).

    So if there is something I'm missing regarding passing an object with NetworkObject component to an ServerRpc method which is causing it to be null, then fixing this other issue could help me as well
     
  3. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    OK I think I actually found the issue. I tried messing around with the object that I was sending asa parameter to a ServerRpc call and then I saw that when I tried to send an object which didn't implement INetworkSerializable, I would get an error message in Unity. Then I realized that I didn't fully implement the
    NetworkSerialize method on My HexGridChunk class, I was only serializing one int. I'm guessing this was the cause of my issues and that I need to implement the serialization of all of the class' fields before I can pass it along to a ServerRpc method.
     
  4. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    I now tried to move the instantiation and spawn back outside of the ServerRpc call and It's working... I have no idea why but yesterday when I tried to spawn the object outside of a ServerRpc call I had errors. I guess I must've done something else which fixed the issue but I have no idea what it was :confused: