#Forcing a player's game object as active via RPC

3 messages · Page 1 of 1 (latest)

lucid python
#

I got the following Code to enable locally a gameobject (canvasUI) on a client once some client interacts with an item:

[SerializeField] 
private GameObject _questPanel;

[SyncVar(hook = "LobbyStart")] 
private bool _activateSideQuestPanel;

public void GameLobbyStart()
{
    if (isServer)
    {
        _activateSideQuestPanel = !_activateSideQuestPanel;
    }
    else
    {
        CmdLobbyStart();
    }
}

[Command]
private void CmdLobbyStart()
{
    _activateSideQuestPanel = !_activateSideQuestPanel;
}

private void LobbyStart(bool oldValue, bool newValue)
{
    _questPanel.SetActive(newValue);
    Debug.Log("Activate???");
}

The methods are called correctly and I can see in the logs "Activate???" on Client2 when Client1 interacts, meaning that LobbyStart() does run. But only the local client that executed the method GameLobbyStart() sees the object as active. Any idea why? I understand due to authority a player can't interact with another player's game objects but since we are telling the players to locally run a method, I would expect it to work?

lucid python
#

I forgot to mention that this Script above is attached to the Player Prefab. I'm starting to believe this might be the cause of the issue?

#

Okay found my answer on an old thread:

Keep in mind that the method is bound to the object where the NetworkBehaviour is attached to. So if Player1 sends a command “CmdMoveForward” the command is executed on the server only on the object that is accociated with Player1. Likewise when the server calls a ClientRPC methhod RpcMovePlayer on the object accociated with Player3, this method is called on all clients on the Player3 object.```

https://discussions.unity.com/t/when-and-how-to-use-clientrpc-and-command/196261/2