Author Topic: Connection to TNet Server Lost When Loading New Scene  (Read 4691 times)

Dennis59

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Connection to TNet Server Lost When Loading New Scene
« on: March 10, 2013, 11:17:05 AM »
I currently have two scenes in my game. The first is to display the various splash screens and the main menu.  The second will be the first level of the game and is currently nothing more than a capsule player that can move around on a cube surface.  When the player clicks on the join server button located on the main menu (first scene) the connection to the server succeeds and the new scene is loaded using TNManager.JoinChannel(...) and the new scene is displayed correctly.  However, at the same time the client is disconnected from the TNet server.  The TNManager script is attached to game objects in both scenes although I've tried only on the first with the same results.

Must be missing something here.

Dennis59

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Connection to TNet Server Lost When Loading New Scene
« Reply #1 on: March 10, 2013, 01:05:43 PM »
After doing some digging it was found that TNManager was being unloaded when the first scene was unloaded before loading the second.  No big surprise there, I suppose.  The fix was to remove TNManager from the second scene, add it to a stand alone GameObject in the first scene and add the following to a script in that GameObject:

  1.         void Awake ()
  2.         {
  3.                 DontDestroyOnLoad(transform.gameObject);
  4.         }
  5.  

TNManager becomes persistent and remains loaded regardless of which scene is active.  Here is a link to the post where I found this http://answers.unity3d.com/questions/8354/scene-connecting.html

If anyone has another solution I would be interested in hearing about it.  Thanks.
« Last Edit: March 10, 2013, 01:30:43 PM by Dennis59 »

broknecho

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Connection to TNet Server Lost When Loading New Scene
« Reply #2 on: March 10, 2013, 02:09:13 PM »
Hey Dennis59!

TNManager actually does do the DontDestroyOnLoad in it's Awake() method so your extra:
  1.     void Awake ()
  2.     {
  3.         DontDestroyOnLoad(transform.gameObject);
  4.     }

is NOT needed.

The other part you have right is the
Quote
add it to a stand alone GameObject in the first scene
part.


So in your first scene have a TNManager GameObject with the TNManager component added to it and it should persist throughout your scenes.

Dennis59

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Connection to TNet Server Lost When Loading New Scene
« Reply #3 on: March 10, 2013, 03:57:19 PM »
broknecho, thanks for the input.  I'll have to give that a try again.  I believe that what I was doing but perhaps not, so I'll give it another go.  At the moment I have the two scene structure I mentioned pretty well torn up to add a Lobby scene so between work and kids it may be a day to two before I can comment back.  Thanks for the input.