I am simply trying to identify the host of my session so i can run some spawning logic on their system only when a session is created. I am using the new widgets to create a session. Here is the code called from my “OnJoinedSession” Event:
public void StartLocalGame()
{
// Find the local player object
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
{
// The host (server) will be the one that owns the NetworkObject
NetworkObject hostObject = NetworkManager.Singleton.LocalClient.PlayerObject.GetComponent<NetworkObject>();
SceneTransitionManager.sceneTransitionManager.RegisterCallbacks();
GameObject go = Resources.Load<GameObject>("GameServer");
var instance = Instantiate(NetworkManager.Singleton.GetNetworkPrefabOverride(go));
var instanceNetworkObject = instance.GetComponent<NetworkObject>();
instanceNetworkObject.Spawn();
Debug.Log($"Host is: {hostObject.OwnerClientId}");
}
else
{
Debug.Log("You created a session but you are not the host for some god forsaken reason");
}
}
As predicted my else statement is being called even though i am the creator of the session.
Is there another approach i need to take to find the session host in the new Multiplayer widget system?
Note that IsServer
is true for the host. But a dedicated server will not have a local player object. If you don’t plan on creating a dedicated server you can omit the IsServer
check, otherwise you need two code paths for host and server.
I would say the session hasn’t actually started then. We’re missing the full callstack to debug this. Check if NetworkManager.Singleton.StartHost()
was called before the OnJoinedSession
event got raised.
If i try to use NetworkManager.Singleton.StartHost() manually it just gives me a warning that the host cannot be started because another instance already is active. I also get the same problem while i am fully in a session, if i put if(IsHost) in Update(). No matter how long I wait after creating a session, it will return false.
I did some further debegguing with this:
if (NetworkManager.Singleton.IsHost)
{
Debug.Log($"Host is: {hostObject.OwnerClientId}");
}
else
{
Debug.Log("This is not the host.");
}
// Alternatively, if you need to get the host player or the server:
if (NetworkManager.Singleton.IsServer)
{
Debug.Log("This machine is the server.");
}
if (NetworkManager.Singleton.IsClient)
{
Debug.Log("This machine is a client.");
}
// If you need to find the host client from a client, check for the clientId of the host
if (NetworkManager.Singleton.IsClient)
{
ulong hostClientId = NetworkManager.Singleton.LocalClientId; // This will give you the local client ID of this instance.
Debug.Log($"Client {hostClientId} is connecting to the server (host).");
}
This will print as follows when I press Create Session:
Does it say the port is active or that a session is already running? Either way, this is the problem you need to solve. Sounds like a previous session did not shut down correctly, or you call StartXxx multiple times on the same instance.
Do you have NetworkManager duplicates? Do you destroy the NetworkManager? In either case, put the NM in the first scene and load the previous first scene from that to avoid the NM duplicating. Destroying it commonly leads to issues respectively indicates architectural problems leading to issues.