Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fredpointzero · Dec 13, 2011 at 02:08 PM · networklevel

Client disconnected between Application.LoadLevel and OnLevelWasLoaded

Hi everyone, I have a basic network level loading question.

To test the project, I use two copies of my unity project and use one Unity instance to launch the server, and the other one to run the client.

I followed the guidelines described in Unity's documentation

I have a scene with one gameobject and a camera. The gameobject has a component NetworkManager. On the client, it instantiates a NetworkClientManager and on the server it instantiates a NetworkServerManager.

I successfully managed to connect the client and the server. So I tried to load a level from the server, but the client get disconnected during the level loading.

This is the server code

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(NetworkView))]
 public class NetworkServerManager : MonoBehaviour
 {
     private int LastLoadedLevelPrefix = 0;
     public NetworkConnectionError NetworkStatus {get;private set;}
     
     void Awake()
     {
         // Network group for network management
         networkView.group = 1;
     }
     
     void Start()
     {
         // Initialize server when NetworkServerManager is started
         NetworkStatus = Network.InitializeServer(10, 25000);    
     }
     
     void OnPlayerConnected(NetworkPlayer player) 
     {
         Debug.Log("Player connected " + player.ToString());
     }

     // This is called on the server
     void OnPlayerDisconnected(NetworkPlayer player) 
     {
         Debug.Log("Player disconnected " + player.ToString());
     }
     
     void OnGUI()
     {
         if (NetworkStatus == NetworkConnectionError.NoError && GUI.Button(new Rect(0, 0, 128, 64), "Load Level")) 
         {
             Network.RemoveRPCsInGroup(0);
             Network.RemoveRPCsInGroup(1);
             networkView.RPC("LoadLevel", RPCMode.AllBuffered, "main", LastLoadedLevelPrefix + 1);
         }
     }
     
     [RPC]
     void LoadLevel(string levelName, int prefix)
     {
         LastLoadedLevelPrefix = prefix;
         
         Network.SetSendingEnabled(0, false);
         Network.isMessageQueueRunning = false;
         
         Network.SetLevelPrefix(prefix);
         Application.LoadLevel("main");
         Debug.Log("Loading level");
     }
     
     void OnLevelWasLoaded(int level)
     {
         Debug.Log("Sending data");
         Network.isMessageQueueRunning = true;
         Network.SetSendingEnabled(0, true);
     }
 }


And now, the client side code :

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(NetworkView))]
 public class NetworkClientManager : MonoBehaviour
 {
     public NetworkConnectionError? NetworkStatus { get; private set;}
     
     public void Connect(string ip)
     {
         NetworkStatus = Network.Connect(ip, 25000);
     }
     
     public void Disconnect()
     {
         Network.Disconnect(200);
     }
     
     void Awake()
     {
         networkView.group = 1;
     }
     
     void OnEnable()
     {
         NetworkStatus = null;
     }
     
     void OnDisable()
     {
         Disconnect();
     }
     
     void OnConnectedToServer()
     {
         Debug.Log("Connected to server");
     }
     
     void OnGUI()
     {
         if (GUI.Button(new Rect(0, 0, 128, 64), "Disconnect")) 
         {
             Disconnect();
         }
     }
     
     void OnDisconnectedFromServer(NetworkDisconnection info) 
     {
         Debug.Log("Disconnected " + info.ToString());
         NetworkStatus = null;
         Destroy(this);
     }
     
     [RPC]
     void LoadLevel(string level, int levelPrefix)
     {
         Network.SetSendingEnabled(0, false);
         Network.isMessageQueueRunning = false;
         
         Network.SetLevelPrefix(levelPrefix);
         
         Application.LoadLevel(level);
         Debug.Log(Network.peerType);
     }
     
     void OnLevelWasLoaded(int level)
     {
         Debug.Log(Network.peerType);
         ObserverManager.ObserverCamera.gameObject.active = true;
         
         Debug.Log("Sending data");
         Network.isMessageQueueRunning = true;
         Network.SetSendingEnabled(0, true);
     }
 }

The logs on the server are:

  • Running as server. Player ID is 0.

  • New connection established (127.0.0.1:62466)

  • Network protocol version -2130706432 connected

  • New scope index 0 is now in scope for SceneID: 3 Level Prefix: 0

  • Allocated 2 batches of size 50 for player 1

  • Sent initalization to player 1

  • Player connected 1

  • 0 RPC function were removed with RemoveRPC

  • 0 RPC function were removed with RemoveRPC

  • Loading level

  • Sent RPC call 'LoadLevel' to all connected clients

  • Added RPC 'LoadLevel' to buffer.

  • Sending data

  • A client has disconnected from this server. Player ID 1, IP 127.0.0.1:62466

  • Player disconnected 1

And on the client :

  • Running as client. No player ID set yet.

  • Connected to 127.0.0.1:25000

  • Connected to server

  • Set player ID to 3

  • Received RPC 'LoadLevel'- mode 2 - sender 127.0.0.1:25000

  • Relay RPC - name: LoadLevel - mode 2 - sender 127.0.0.1:25000

  • Client

  • Disconnected

  • Sending data

Well, you can see on the client the two logs : "Client", followed by "Disconnected" which are the two calls to Debug.Log(Network.peerType) on LoadLevel and OnLevelWasLoaded

But the OnDisconnectedFromServer method is never called ...

What am I missing ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Dec 13, 2011 at 05:22 PM

Are you actually disconnected? It seems Unity have some kind of bug that peerType won't show the actual status in OnLevelWasLoaded. However you should make sure that your script isn't destroyed(DontDestroyOnLoad) due to levelload. You have a "Disconnect()" in your OnDisable method so if the script get's destroyed you will cause a disconnect.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fredpointzero · Dec 14, 2011 at 12:46 PM 0
Share

I found the issue : $$anonymous$$y Network$$anonymous$$anager is not destroyed when a level load, and the NetworkClient$$anonymous$$anager is added to the same gameobject.

For an unknown reason, the OnDisable method is called on NetworkClient$$anonymous$$anager after the level was loaded and so calling a disconnect.

Thanks for your guess !

avatar image
0

Answer by Hardcore Games · Jun 26, 2013 at 08:30 PM

To fully understand how the Network in Unity3d is working is with an example.

Go to this tutorial. You'll never get something better explained!

http://www.youtube.com/watch?v=m-iEOIXT19k&list=PLC90282759FD90F77

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Network Level Loading Freezes Everything 1 Answer

Level Joining Issue 1 Answer

Uploading user created levels from webplayer 0 Answers

Networking: how to sync NetworkViewIDs for level objects on peers? 4 Answers

Network With Maps 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges