1
votes

When using NetworkManager.ServerChangeScene() to switch from stage 1 to stage 2, the player gets destroyed and is not present when stage 2 loads. There are no errors and the scene does change successfully.

What could cause this?

Here is my code to change scenes:

[ServerCallback]
public void LoadOnline (string sceneName) {

    NetworkManager.singleton.ServerChangeScene (sceneName);
}
1
is it possible you need to use DontDestroyOnLoad ? - Fattie
you know, it seems pretty unlikely there's such a fundamental bug. by "player" do you mean a GameObject in the game? is it in SceneA? if so when you load SceneB, that game object will be destroyed. Everything in SceneA will be destroyed when you load SceneB. I assume by "stage 1" you mean a Unity Scene. (If you mean something else - sorry!) - Fattie
is it possible you're in need of a Preload scene? (unrelated essay which mentions it stackoverflow.com/a/35524924/294884 ) - Fattie
actually here is an essay on that stackoverflow.com/a/35465978/294884 - Fattie
yes, it is quite strange in Unity ................ in some sense, you could argue that the DEFAULT should be "dont destroy". why the hell would you want to destroy things? if you DO want to destroy something (example, "one road is changing to another"), sure, you would manually tell it to destroy the first road in question. Anyway .................... it is what it is! :) hope it helps somehow - Fattie

1 Answers

1
votes

In order to maintain a persistent game object (or player) from stage 1 to stage 2 you need to add a call to DontDestroyOnLoad in the Awake or Start method of the object. See https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html.

This will also present a challenge in how you previously set the player's position in the scene file. One approach I've been using is to create an empty game object in each stage with a tag (like RespawnPoint) that is used to set the position of the player after the scene has changed (which could be handled somewhere after your call to ServerChangeScene).