- Home /
Network: Connect & LoadLevel on client side (bugs)
Hi
I've been searching for a couple of hours now on this problem:
I have a GameObject Administration with 2 scripts attached. 1 with this code, so it won't be destroyed during the game:
void Awake()
{
// Don't destroy the object, so it can be used in other scenes
GameObject.DontDestroyOnLoad(this);
}
The other scripted is my own networkScript.
In the mainmenu (scene), you can choose to be a server or client and depending of the choice, the game will load the Server or Client scene. In the client scene, I have this in the OnGUI() of an attached script:
// "Connect to server" button
if (GUILayout.Button(txtBtnConnectToServer))
{
ConnectToServer();
Application.LoadLevel((int)levelToLoad_ConnectToServerBtn);
}
ConnectToServer() calls Network.Connect("ip", "port") in the networkScript (that is attached to the administration object) This works fine. Server and clients can see each other, attack each other...
Now, as you can see, it's possible that the client is still connecting, but already loads the next level. So what I've tried:
In the Client scene, I've changed the code to this:
// "Connect to server" button
if (GUILayout.Button(txtBtnConnectToServer))
{
ConnectToServer();
}
In the networkScript (attached to Administration), I have this:
void OnConnectedToServer() { Application.LoadLevel((int)levelToLoad_ConnectToServerBtn);
}
This doesn't work correctly. All players can see the new spawned player, but a new player can't see the old players already connected to the network. I started looking in the editor (as a client). If I connect, I see the spawned players for a second, than loads the level, and removes all the old players in its own scene.
I don't use OnConnectedToServer() in other scripts. If I change the code from the server side in something that's equal to this (but with OnServerInitialized()), it will work for the server.
I also tried to bind Application.LoadLevel((int)levelToLoad_ConnectToServerBtn) to a key. Just let the client connect to the server, wait a few seconds, and pressed the key.
It seems that Application.LoadLevel() removes all spawned network objects in your own scene?
Answer by Xullripper · Jun 01, 2012 at 05:40 AM
Ok, after another couple of hours debugging and testing, I finally found the problem!
The player object (and it's weapons, inventory...) that spawns in the network needs the DontDestroyOnLoad(this) too.
I think this solution was obvious, since every connected player have their own instances of spawned network players in their scene. LevelLoad() deletes all objects in your own scene, so the instances of spawned network players too.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072222im_/https://answers.unity.com/themes/thub/images/avi.jpg)