Totally lost with UNET – Client to Server Networking

Updated on September 15, 2018 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on September 15, 2018

Hello there,

 

I’m trying to recreate a bomb system like CS:GO has, but I have been trying for some time now, and I’m at the edge of giving up. When I google this problem, I get some answers but none seem to work for me, like authorizing manually, but what should I authorize? I have no player in my test-scene.

I have had everything working multiple times, like sound, UI etc… But one thing just keeps messing me up and that’s client to server. Everything always works fine when the server player does something, but the other way around I never got it working.

A quick example of what I want to do:

A player presses “E” and then music starts playing. This code is on the “Bomb” prefab with a network Identity etc…

 

When the host presses “E”, every client starts playing the sound too! But the other way around I always got stuck at authorisation errors.

My current very simple code:

public class Bomb : NetworkBehaviour {
    public AudioSource bombMusic;
    [SyncVar]
    public float timeLeft = 40f;
    [SyncVar]
    public bool hasBeenPlanted;
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.P))
        {
            if (isLocalPlayer)
            {
                RpcStartBombSequence();
            } else
            {
                CmdStartBombSequence();
            }
        }
    }
    [ClientRpc]
    void RpcStartBombSequence()
    {
        hasBeenPlanted = true;
        bombMusic.Play();
    }
   
    [Command]
    void CmdStartBombSequence()
    {
        RpcStartBombSequence();
    }
}

 

The error:

 

Also, the Syncvar updates on all clients when the server changes it, but not the other way around. How could I fix this?

 

Thank you very much, I appreciate all replies.

 

-Tristan

  • Liked by
Reply