0
\$\begingroup\$

I have a scene for lobby. When player connects to server he gets a lobbyCharacter, which is an empty gameobject with a script on it that create UI element with the players name. In lobby players can select character they want to use.

Now when starting game, i would like to switch scene to the game scene when all players are ready. I have been reading the API, documentation, and different stack questions, but i cant seem to find any info on how this should work. On NetworkManager i can do scene schange which will be changed on all clients. But how do i persist data over multiple scenes? And if the lobby characters are destroyied how are the game characters instansiated according to the choises made in lobby? How does the client connection persist between multiple scenes?

Do i just manage the character locally and use syncHooks to synchronize over the server? Do i need to create somesort of container gameobject that holds the player connection information and some extra stuff?

EDIT

Reading the soruce code of mirror i fouind that NetworkManager holds the connection of the client, sort of. So now the issue has come down to replacing the prefab for the connection.

I have the following code:

 public void switchScene()
    {
        GameObject obj = NetworkClient.connection.identity.gameObject;

        NetworkServer.ReplacePlayerForConnection(NetworkClient.connection, Instantiate(shipPrefab));

        NetworkServer.Destroy(obj);
        ServerChangeScene(gameScene);
    }

This should switch the prefab for connection and then switch scene, but i keep getting error

InvalidCastException: Specified cast is not valid.
Mirror.NetworkIdentity.SetClientOwner (Mirror.NetworkConnection conn) (at Assets/Mirror/Runtime/NetworkIdentity.cs:362)
Mirror.NetworkServer.InternalReplacePlayerForConnection (Mirror.NetworkConnection conn, UnityEngine.GameObject player, System.Boolean keepAuthority) (at Assets/Mirror/Runtime/NetworkServer.cs:837)
Mirror.NetworkServer.ReplacePlayerForConnection (Mirror.NetworkConnection conn, UnityEngine.GameObject player, System.Boolean keepAuthority) (at Assets/Mirror/Runtime/NetworkServer.cs:697)
LobbyManager.switchScene () (at Assets/LobbyManager.cs:51)

And i dont understand why this is happening. New prefab has networkIdentity and networkTransform. Old prefab only has network transform. Up until this point everything wors, but once i want to switch scenes, it breaks.

\$\endgroup\$
3
  • \$\begingroup\$ How have you tried applying past Q&A about persisting data between scenes in Unity to this situation? Did you run into any particular snag making those past answers work for your needs? \$\endgroup\$
    – DMGregory
    Dec 1, 2020 at 22:15
  • \$\begingroup\$ After investigating the mirror-network sourcecode I understand that the NEtworkManager itself holds the connection of the client. But now i have the issue of switching player character, \$\endgroup\$
    – Marko Taht
    Dec 2, 2020 at 20:19
  • \$\begingroup\$ There is even an example in the mirror project that includes a lobby system that does exactly what you are trying to achieve. It is called Room \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 15:41

1 Answer 1

1
\$\begingroup\$

Had the same issue. You will have to get a networkbehaviour on your player with GetComponent and take the connectionToClient. Only works when your switchScene() is running on the server, obviously.

\$\endgroup\$
1
  • \$\begingroup\$ So i need to tell the server to switch scene. So if server does it i can can rebind everything i want. \$\endgroup\$
    – Marko Taht
    Jan 5, 2021 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .