But this piece of code does not work on the client side. I feel like I’ve read the docs 5 times but I still don’t quite understand how I am supposed to approach loading a scene on the clients and have everything synced up properly.
There is no scene loading on the client side. If the client isn’t automatically loading the scene the server has currently loaded, there’s some issue with that in your code / project / build.
For example if you call StartHost() and immediately call LoadScene() on the server (rather than in OnNetworkSpawn for example) this may cause issues if I interpret a recent forum post about scene loading correctly. Or attempting to load scenes on the client side. Or server/client builds differ (hint: use Multiplay package or ParrelSync).
In any case, if the server loads a scene, all clients will load that scene too. If a client joins the game late, then that client will load the server’s current scene. If you merely want a client to tell the server to change scenes, then call a ServerRPC method from the client and let the server change the scene.
Hey! Thanks for getting back to me, that clears up a lot about scene management with netcode! You were right about it being an issue with my code, I had forgotten to set the host relay data.
Hey can give you an example of what you mean by writing it in the onNetworkSpawn function? I currently have a setup where I call starthost then load scene immediately and it causes a lot of issues. I just want to see what the optimal setup would be, using a join script in my example.
My situation is not standard. My client and server are not running the same scene. The server is basically a dashboard showing multiple clients. But the clients are in their own scene. There is also no interaction between the clients, only between the clients and the server.
So I need to load a different scene on the client. How can I do this?
Both server and clients have to be in the same single-loaded scene. This scene however could be empty. Then, on the server-side and client-side separately you additively load the server and client side scenes respectively, but by using UnityEngine.SceneManager (specifically NOT using the network scene manager).
You will also have to implement scene validation callbacks so that the clients don’t load the server-side additive scenes, which will happen by default. You may also (or merely) have to switch scene load mode to “additive”. Not sure if you need to do both or just one.
Read through all the pages listed under Integrated Scene Management since you’ll need to have a firm grasp of these concepts and behaviours.
Thanks for your quick reply. I have started with those pages and figured the code snippet for the server would also work for the client. I’ll dive deeper into this.
Just a quick question, on this first page there is a warning that:
’ If you use the UnityEngine.SceneManagement.SceneManager during a netcode enabled game session, then those scenes won’t be synchronized with currently connected clients.’
Will I still be able to exchange data between the client and server, for example using RPCs? I won’t have any NetworkObjects that require automatic synchronisation, but I still need to inform the server about scores and other data.
Yes, you can send messages (see: custom (named) messages). But you cannot use RPCs in objects of non-networked scenes because they’ll only work in NetworkBehaviour subclasses which in turn require a NetworkObject on the GameObject or one of its parents - but you can’t have those in non-networked scenes.
What you can do to enable network sync is to put all your NetworkObject instances in the root of the single-loaded scene so that they exist for both server and clients. The additively loaded content can get those instances eg by using FindObjectOfType or using a ‘components registry’ aka dependency lookup, which is comparable to dependency injection but easier to implement and debug. And to distribute received RPCs you provide events that classes can subscribe to, eg OnDataReceived.
Sorry, wrote long post about that it did not work, but it looks like this was caused by a scene-name that was removed in one of the scripts.
Now it works fine.
No, I didn’t know that yet. Thanks! That will make testing a lot easier.
And about my ‘It works fine’ statement, that’s just the loading of scenes right now. Next step is actually sending data And the validation checks to make sure the client does not see the server scene.
Hmmm, the package doesn’t seem to exist (anymore) or has a different name. In the Unity Registry section of the Package Manager I only see:
Mulitplay
Multplayer Services
Multiplayer Tools
(Unity 2022.3.20f1)
Oh nevermind, minimum version is UNity 2023. Ugraded my project and now of course I’ll need to fix other people’s compile errors…
Assets\VRMPAssets\Scripts\Gameplay\MessageBoard\NetworkMessageBoard.cs(5,50): error CS0234: The type or namespace name ‘SpatialKeyboard’ does not exist in the namespace ‘UnityEngine.XR.Interaction.Toolkit.Samples’ (are you missing an assembly reference?)
If you’re having trouble with MPPM I’d recommend ParrelSync as an alternative, it works with older versions of Unity.
I’ve not looked into conditional additive scene loading but with scene management disabled you can have network objects in non-network scenes, just with that the onus is on you to keep track of what’s loaded and where. I mention this joostbos as it offers another approach for what you’re after but additive scenes may be the simpler way to go.
Thanks, good to know. There might be a situation where the clients swtch between scenes while the teacher scene remains active. So then I’ll need to load and unload scenes additively. Or load them as a single scene and jump back to the selection scene.