- Home /
UNET - Keep connection to client when changing scenes
I have a question to which I am not able to find any answer. So I'm making a pretty bulky game and I need to have a ton of scenes. I'm not very well versed in Unity, so maybe it's the user who's at fault :)
I have a TCP connection between a server and multiple clients using NetworkTransport API. So the problem is, when I do this on the client, I lose the connection:
Client:
StartCoroutine(LoadNextSceneAsync("Level 2")); // Simply loads async scene through SceneManager
And whenever the scene is loaded, the client simply disconnects, even though the NetworkManager gameobject, that has the client script, is set to DontDestroyOnLoad(); Another problem is that if the gameobject, that is not destroyed, transfers to the next scene, how do I reference it in the scripts within that new scene, because these objects don't exist in that scene, and I can't compile the code. For example:
// Scene 1:
// Network Game Objects have DontDestroyOnLoad() casted on them.
SceneManager.LoadSceneAsync("Level 2");
// Scene 2:
/* Has other Game Objects and scripts that call for variables in network objects from Scene 1 that are hopefully now transferred*/
PlayerName = NetworkManager.GetClientName(); // NetworkManager object doesn't exist in scene 2 two until transferred from Scene 1.
Question: How to properly manage scenes within the client, and not lose connection to the server, while at the same time transferring the NetworkManager Game Object through all scenes?
Answer by niocy · Dec 23, 2017 at 04:08 PM
I think you have 1 player as a host. I did it by using low lvl api. Host tells clients that scene is changing and clients can start loading the scene. Host waits for everyone to load the scene to 90% after which all the clients tells the hosts that they are done loading and ready to change (async operation). Host is the first one to change scene at this point, sends message to all clients that you can change scene and starts to check if everyone has loaded. After seeing every client has sent a succesful scenechange message to host, host can start to spawn crucial objects. Because if you start spawning objects before everyone has loaded, those objects will spawn in the wrong scene. Clients should be marked as not ready during this process.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Pushed back when applying NetworkTransform 1 Answer
UNET Display Connected Player Names inGame UI 0 Answers