• 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
Question by TheFlyingBastard · Apr 29, 2016 at 03:23 PM · networkingscriptingbasicserrormessageconnecting

How do I handle connection errors in a user friendly way?

I'm trying to create a game where players come together in a lobby before the actual gameplay and for that I have made my own Host Game and Join Game buttons. If a host is running, you can enter the IP address and the port and the client will connect fine. T$$anonymous$$s is done through my custom NetworkLobbyManager:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class NetworkLobbyManagerCustom : NetworkLobbyManager {
 
     public void setupHost () {
         NetworkManager.singleton.StartHost ();
         Debug.Log ("Host started");
     }
 
     public void unloadHost() {
         NetworkManager.singleton.StopHost ();
         Debug.Log ("Host stopped");
     }
 
     public void setupClient (string ip, int port) {
         NetworkManager.singleton.networkPort = port;
         NetworkManager.singleton.networkAddress = ip;
         Debug.Log ("Connecting...");
         NetworkManager.singleton.StartClient ();
     }
 
     public void unloadClient() {
         NetworkManager.singleton.StopClient ();
     }
 }

If you enter some nonsense into the IP box the Console will print "DNS resolution failed" and "UNet Client Error Connect Error: 11" error messages as expected. If you enter a correct IP address, but the server isn't running, the scene just restarts.

I'm not quite satisfied with t$$anonymous$$s - I need to be able to tell the user what he did wrong. I've been looking through the documentation to see what I could find and it seems like the OnFailedToConnect() function is what I'm looking for.

Just to try it out, I wanted to copy the following function from the documentation:

 void OnFailedToConnect(NetworkConnectionError error) {
     Debug.Log("Could not connect to server: " + error);
 }

but I had no idea where to put it. I tried putting it in the script for my NetworkLobbyManager w$$anonymous$$ch, as you can see above, has a call functions such as StartClient() to succesfully connect to a server - so it made sense to me that I would put the OnFailedToConnect() function there too.

Now when I enter the wrong data or when my server isn't running, StartClient() gets called as per usual and the game will try to connect to the server, but once it fails, OnFailedToConnect() doesn't seem to be called. At least not in the NetworkLobbyManager.

Am I putting OnFailedToConnect() in the wrong place? Or am I supposed to use a different function altogether to handle connection failures and errors? Hell, am I going about t$$anonymous$$s the right way?

Thanks in advance for your help.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Getting my Actual IP? (not 120.0.0.1) 1 Answer

Networking not working 0 Answers

Question about UNET 0 Answers

Getting a reference on a client when another client connects 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