Search Unity

Scene Object Not Spawning on Scene Load

Discussion in 'Multiplayer' started by Tinjaw, Oct 21, 2015.

  1. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    I am referencing this page of the docs.

    Then, when the scene is fully loaded, NetworkServer.SpawnObjects() is called to activate these networked scene objects. This will be done automatically by the NetworkManager when the server scene finishes loading...

    I am not seeing this behavior in 5.2.1f1.

    I load a scene (on the host) that has an object with a network identity attached. That object is disabled when the scene finishes loading. It is not being activated, as I believe it should, based on the documentation. In the prior scene I have a Network Manager that is not unloaded with the scene (so it is still active on scene load).

    Can anybody shed some light on my issue?
     
    Last edited: Oct 26, 2015
    ZuhairGhias likes this.
  2. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    Bump. I'm stumped.
     
  3. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
  4. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    Same behavior in 5.2.2f1.

    Can anybody besides me reproduce this? Is it a bug or is my project not doing things the way it should?
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    the networkidentity must be on a root object in the scene, not a child object.
     
  6. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
  7. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I have the same issue. My scene objects do not automatically load either (On the LocalHost they are enabled, but on LocalClient they are disabled and never "spawned"). They have NetworkIdentity on the root of the object and the documentation says sceneObject spawning should be automatic... so I'm not exactly sure what is going wrong.

    As a solution I have a persistent singleton object with this code that seems to fix it. Unsure why this bandaid is needed though, I feel like I'm not doing something right or there is some bug preventing things from spawning (I've had issues with NetworkTransform and SyncList's preventing scene objects from spawning. http://forum.unity3d.com/threads/bug-synclistfloat-crashes-networkanimator.361187/).

    In any case create a persistent object and slap this code on it and see if this fixes your issue.

    Code (csharp):
    1.  
    2. private void Awake()
    3. {
    4.    DontDestroyOnLoad(this);
    5. }
    6.  
    7. private void OnLevelWasLoaded(int level)
    8. {
    9.    if(isServer)
    10.    {
    11.       NetworkServer.SpawnObjects();
    12.    }
    13. }
    14.  
     
    Tinjaw likes this.
  8. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @Zullar Thank you very much for your workaround. I'll wait for @seanr to reply, but then I think I will report this as a bug.
     
  9. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @Zullar

    I tried your workaround and I only have partial success. The object is activated on the LOCAL client. However, on remote clients it is still disabled and I get an error:

    Spawn scene object not found for 1
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
     
  10. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    OK. Few more questions.
    -Are you using a custom NetworkManager?
    -Are you using NetworkTransform?
    -Are you using SyncLists?

    Somehow for me it seemed these things affected the spawn messages that are sent (not 100% sure). I saw really strange things. Doing things like deleting my SyncList would cause sceneObjects to spawn properly, or re-arranging components in the object heirarchy could also turn the scene object spawning problem on/off. If we can pinpoint the exact scenario that is causing our sceneObjects to fail to spawn then hopefully SeanR can help fix the bug.
     
  11. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    In my actual project I am. But in my example scene (linked to above) I am not.

    No, I am not.

    In my actual project I am. But in my example scene, I am not.
     
  12. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    I cannot repro this is 5.2.2p1
     
  13. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    I'll grab the patch and try with my project linked above.

    @seanr BTW, did you test w/ my project?

    Thanks
     
  14. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  15. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    First off, thank you @seanr for looking into this matter. I appreciate the support.

    I have downloaded 5.2.2p1 and it is an improvement. Now, the object is enabled on the local client. However, it is still deactivated on any remote client.

    You can see this in my example project by building and running as host, then pressing the button to load scene two. Then, in the IDE, run and choose to be a client. Then click the button on the client to load the same scene. You will see the cube is created, but it is disabled.

    If I remember correctly, it wasn't even being enabled on the local client, so this is an improvement. I guess I should check the bug list and see what was included in p1. Maybe I can find the existing bug and add this info.
     
    SevenG3P likes this.
  16. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    The only fix in the patch that may be related, that I can see is this one. Otherwise I don't know what changed the behavior with the local client.
     
  17. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Are you saying this happens when you run "Application.LoadLevel(1);" on the client?

    That will not trigger networked objects in the scene to be enabled. This page should probably mention that..

    http://docs.unity3d.com/Manual/UNetSceneObjects.html

    NetworkManager.ServerChangeScene() is used to do this.
     
  18. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @seanr If you make one change to LoadIt.cs...

    Code (CSharp):
    1.     public void OnClick()
    2.     {
    3.         //Application.LoadLevel(1);
    4.         NetworkManager.singleton.ServerChangeScene("Scene Two");
    5.     }
    Then build and run as host. Scene Two is loaded and cube is active on local client. Then run in the IDE and choose client. Upon being connected this remote client automatically loads Scene Two, the cube is present but still disabled.
     
  19. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @seanr
    Do you have a working example I can import? As of now, I haven't see it work properly and plan to file it as a bug.

    Thanks
     
  20. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    I cannot reproduce this problem. Feel free to make a bug submission, then QA will look at it.
     
  21. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @seanr

    With a little more tinkering, I think I determines why I see the bug and you don't. We were doing things in a different order.

    But will NOT surface if you do things in this order:
    1) Run EXE
    2) Start Host on EXE
    3) Start Client in IDE
    4) Connect client
    5) push button

    But will surface if you do things in this order:
    1)Run EXE
    2) Start Host on EXE
    3) Push button
    4) Start Client in IDE
    5) Connect client

    So, if a client is NOT connected and the host loads a scene, when the client connects it will load the scene, but will not activate scene objects.

    If I client is connected and a host loads a scene, the client will load the scene and activate the scene objects.

    So, I will just make the necessary adjustments in my actual game.

    @seanr Thanks for your assistance.
     
  22. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Last edited: Nov 23, 2015
  23. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    I'm still totally lost on this... In a persistent world game the you always run into what Tinjaw points out above: the client connects after the level is loaded and none of the scene objects will enable "automatically" which is all the docs say.

    putting a command to Network.SpawnObjects(); onto the player prefab being spawned with the newly connected player does not enable the objects.

    In what situation is SpawnObjects() supposed to work?

    To backup, I'm running 5.3.5f1. I have a single scene with the basic network manager in it. The scene has a tree that has a network identity (because it is interactable) and so is disabled for any clients that connect past the host player.
     
  24. helgewt

    helgewt

    Joined:
    Jul 21, 2015
    Posts:
    12
    I think I am experiencing the same issue (Unity 5.5.1f1).
    I have a project with a custom NetworkManager implementation, onlineScene is left unconfigured, and the host/server always changes the scene after it has started.
    When a client connects, it switches to the new scene, but scene objects fail to spawn (remain deactivated) and I see errors similar to "Spawn scene object not found for 1".

    Cause:
    When a client is connected, OnClientConnect is called before processing scene change from the server.
    With the standard NetworkManager, if onlineScene is not configured, this will result in ClientScene.Ready being called.
    Because the client has reported ready, it will receive spawn messages while the new scene is still loading, and the spawn commands will fail.

    Workaround:
    Override OnClientConnect in the custom NetworkManager, do not call ClientScene.Ready and do not call the base implementation.
    Rely on OnClientSceneChanged to call ClientScene.Ready and adding the player after scene has changed (in my project, the server always changes the scene).
     
  25. Amir-Ward

    Amir-Ward

    Joined:
    Jan 26, 2015
    Posts:
    30
    For anyone else who comes across this problem make sure your networked scene GameObjects are set to active before you build the player!! Otherwise you will get these errors. I spent 3 hours trying to work that out.
     
  26. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    268
    I upgraded my Unity 5.4 project to 5.6 and started getting this issue.

    helgewl's answer solved it for me :) Thanks
     
  27. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I have...
    SceneTown (no networked objects)
    SceneDungeon1 (many scene networked objects)
    SceneDungeon2 (many scene networked objects)
    SceneDungeon3 (many scene networked objects)

    If clients connect while in Town then everything is OK. But if the players are in SceneDungeon2 and a new client connects I get all sorts of UNET errors. I'll have to try @helgewl workaround and post how it goes.

    Should we bug report this?
     
  28. Amir-Ward

    Amir-Ward

    Joined:
    Jan 26, 2015
    Posts:
    30
    btw if you've created a prefab with a network identity on it and forgot to add it to the NetworkManager spawnable prefab list before playing in the editor, as a host or client then you will need to delete the prefab, remove all the network components, make the prefab again, play the game, stop the game, put the network components back on and then add it to the spawnable prefab list. This will clear out the errors. And from my previous message also make sure it is set to active on build/play.
     
  29. sunil_behera

    sunil_behera

    Joined:
    Oct 27, 2015
    Posts:
    1
    @Tinjaw

    By the way ,I am not good in eglish.

    Here is the solution:--

    Check your lobby scene or "starting scene" or the scene where you click on "start host" / "join" that there is no such Gameobjects(in LobbyScene) contain "networkidentity", if you have any such Gameobjects(in Lobby Scene), then delete or remove "network identity" for this time and and run the game, this time you will get the Gameobject(in Play Scene) enabled in your "play scene" or "load scene" on client side.
     
    Last edited: Jun 25, 2017
  30. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518

    Thank you very much for taking the time to message me with this information. I appreciate it.