1

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);
}
Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
user3071284
  • 6,955
  • 6
  • 43
  • 57
  • is it possible you need to use `DontDestroyOnLoad` ? – Fattie Mar 01 '16 at 12:51
  • 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 Mar 01 '16 at 14:44
  • is it possible you're in need of a Preload scene? (unrelated essay which mentions it http://stackoverflow.com/a/35524924/294884 ) – Fattie Mar 01 '16 at 14:45
  • actually here is an essay on that http://stackoverflow.com/a/35465978/294884 – Fattie Mar 01 '16 at 14:48
  • @JoeBlow Thanks, I don't know why I thought it didn't need that. After reading all the available information on UNet in the past year or so, it seemed like this was handled automatically. Why would you want the player, which is a required prefab for UNet to work, to be destroyed when going to the next stage... – user3071284 Mar 03 '16 at 01:08
  • i'm not sure precisely what you mean, but, I hope it is working now!! – Fattie Mar 03 '16 at 01:37
  • 1
    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 Mar 03 '16 at 01:38

1 Answers1

1

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).