Hello,
I’m a beginner at Unity3d and networking. I’m creating a UNET multiplayer game by having the host be a client with up to 3 other clients connecting. This is not cloud matchmaking.
I’m having a problem with restarting a game once I’ve cancelled it in the lobby manager using stopHost().
My game is simple:
Scene 0 = Main Menu (not networked)
Scene 1 = Lobby (Custom singleton UNET Lobby NetworkManager for up to four players)
Scene 2 = Game (Networked)
Scene 3 = HighScores (defaults to this after Game is over/calls StopServer and StopHost).
If I enter the lobby from the menu and host a game, the network lobby starts up with an available cancel button. The button calls StopHost() and it does stop the game in the lobby before it starts. This works fine.
It should be noted that the Lobby NetworkManager persists across all screens as Don’t Destroy on Load, Auto Spawn Player and Run in Background are all checked.
When I try and restart the game a second time by clicking the “Play and Host”/StartHost () button I get the following two errors:
-
Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied.
-
StartServer listen failed.
I also get these errors if I play a full game, go to the HighScores screen (which also does StopHost and StopServer) and then back to the Menu to restart another networked game. Only by closing the program and restarting it can one create another new networked game.
Clearly when I start and then cancel the first game attempt, the lobby networkManager isn’t freeing up/disconnecting from the port and maybe other network protocols when I call stophost(). Why is this and should I be using some other commands?
There are other threads on this type of error, but nothing in those threads seems to work here.
Any advice on how I should be cancelling my networked games, freeing my ports and ControllerIDs so they can be restarted successfully?