I want to do a number of things after calling the functions NetworkManager.Singleton.StartHost() and NetworkManager.Singleton.StartClient().
Is there a hook or something to do this?
I want to do a number of things after calling the functions NetworkManager.Singleton.StartHost() and NetworkManager.Singleton.StartClient().
Is there a hook or something to do this?
There are two methods that can be registered: OnClientConnectedCallback and OnServerStarted.
As an example for registering OnClientConnectedCallback, you would do the following:
private void Start()
{
if (IsClient)
{
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
}
}
And then define the callback
private void OnClientConnected(ulong clientId)
{
if (clientId == NetworkManager.Singleton.LocalClientId)
{
Debug.Log("Client connected. You can now call ServerRpc methods.");
// Call your ServerRpc method here or trigger an event to notify other scripts
CallYourServerRpc();
}
}
Make sure the class with the above inherits from NetworkBehaviour and that it has a NetworkObject component