Search Unity

Please help me understand why something in my code is acting like this? (UNET)

Discussion in 'UNet' started by mightybob, Sep 23, 2017.

  1. mightybob

    mightybob

    Joined:
    Mar 23, 2014
    Posts:
    75
    It's not broken, it works. It just kills me as to why it works, and makes me question if I actually know what's going on in the rest of my project.

    So for a while I had a bug in my game, where the BulletImpact prefab, spawned when a bullet collides with an object, was spawned twice for the host player. (and once for the client) Today I decided to get around to figuring it out. There didn't appear to be any logical errors in my code, so I just started commenting out a few lines here and there, adding Debug messages, etc.

    This was the broken bit of code:

    Code (CSharp):
    1. Rpc_BulletImpactFX(); //simply runs BulletImpactFX() for the clients
    2. BulletImpactFX(); //run BulletImpactFX() for the server, because RPC's are run on the clients, not the host ... right?
    AFAIK from reading the docs, a function that starts with Rpc and also has [ClientRPC] in front of it, can only be run from the server/host, and the contents of that function is only run on the clients. That kinda happens, except apparently, from my testing, Rpc is also run on the server. (what..?) I came to this conclusion because comment'ing out the "BulletImpactFX();" caused it to *still* be spawned on the server, even though all that was being called now is an Rpc.

    In the Rpc, there is only one line. Instantiate(). No Network.Spawn or something, just a locally run Instantiate(). Can someone please help me understand why this is happening?
     
  2. HotPhone

    HotPhone

    Joined:
    May 14, 2014
    Posts:
    53
    ClientRpc run on the server, at least when the server is also a client.