'NotServerException: ConnectedClients should only be accessed on server. error
iam testing with multiplayer and get this error
NotServerException: ConnectedClients should only be accessed on server.
Unity.Netcode.NetworkManager.get_ConnectedClients () (at Library/PackageCache/[email protected]/Runtime/Core/NetworkManager.cs:269)
TeamPicker.SelectTeam (System.Int32 teamIndex) (at Assets/Assets/Scripts/TeamPicker.cs:12)
UnityEngine.Events.InvokableCall1[T1].Invoke (T1 args0) (at <ab7de6937a4448b0a6ccd59e9820599a>:0) UnityEngine.Events.CachedInvokableCall1[T].Invoke (System.Object[] args) (at :0)
UnityEngine.Events.UnityEvent1[T0].Invoke (T0 arg0) (at <ab7de6937a4448b0a6ccd59e9820599a>:0) UnityEngine.EventSystems.EventTrigger.Execute (UnityEngine.EventSystems.EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/[email protected]/Runtime/EventSystem/EventTrigger.cs:220) UnityEngine.EventSystems.EventTrigger.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/[email protected]/Runtime/EventSystem/EventTrigger.cs:277) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/[email protected]/Runtime/EventSystem/ExecuteEvents.cs:57) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at Library/PackageCache/[email protected]/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/[email protected]/Runtime/EventSystem/EventSystem.cs:501)
the code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class TeamPicker : MonoBehaviour
{
public void SelectTeam(int teamIndex)
{
ulong localClientId = NetworkManager.Singleton.LocalClientId;
if(!NetworkManager.Singleton.ConnectedClients.TryGetValue(localClientId, out NetworkClient networkClient))
{
//throw new NotServerException("Only the server can find player objects from other clients.");
return;
}
if(!networkClient.PlayerObject.TryGetComponent<TeamPlayer>(out TeamPlayer teamPlayer))
{
return;
}
teamPlayer.SetTeamServerRpc((byte)teamIndex);
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class TeamPlayer : NetworkBehaviour
{
[SerializeField] private Renderer teamColorRenderer;
[SerializeField] private Color[] teamColors;
private NetworkVariable<byte> teamIndex = new NetworkVariable<byte>(0);
[ServerRpc]
public void SetTeamServerRpc(byte newTeamIndex)
{
if(newTeamIndex > 3 ){ return; }
teamIndex.Value = newTeamIndex;
}
private void OnEnable()
{
teamIndex.OnValueChanged += OnTeamChanged;
}
private void OnDisable()
{
teamIndex.OnValueChanged -= OnTeamChanged;
}
private void OnTeamChanged(byte oldTeamIndex, byte NewTeamIndex)
{
if (!IsClient) { return; }
teamColorRenderer.material.SetColor("_BaseColor", teamColors[NewTeamIndex]);
Debug.Log("Clicked");
Debug.Log(teamColors[NewTeamIndex]);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
