Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kurdle_4855 · Feb 16, 2016 at 12:56 PM · c#unity 5multiplayerconnection

Not Connecting to Server

So, i'm making a game in Unity. It's multiplayer. For my specific 'needs' I had to create my own Network Manager. That's what I did. Everything appears to work fine, and it does, except for one thing: It's not connected to the server. Even though i've set up hosting, server running, and connecting, it supposedly is never actually connected. Please help. Here's the code for my Network Manager class: using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.SceneManagement;

enum PlayMenuMode { Home, Connect, Server, Connected, None }

public class PlayMenu : NetworkManager {

 PlayMenuMode menu = PlayMenuMode.Home;
 NetworkClient client;
 int port = 4855;
 string ip = "127.0.0.1";
 short playerConnection;

 void OnGUI()
 {
     if(menu == PlayMenuMode.Home)
     {
         if(GUILayout.Button("Singleplayer/LAN"))
         {
             menu = PlayMenuMode.None;
             Host();
         }
         if(GUILayout.Button("Connect"))
         {
             menu = PlayMenuMode.Connect;
         }
         if(GUILayout.Button("Start Server"))
         {
             menu = PlayMenuMode.Server;
         }
     }

     if(menu == PlayMenuMode.Connect)
     {
         GUILayout.Label("IP To Connect");
         ip = GUILayout.TextArea(ip);
         GUILayout.Label("Port To Use");
         port = int.Parse(GUILayout.TextArea(port.ToString()));
         if(GUILayout.Button("Connect")){
             Connect();
             menu = PlayMenuMode.Connected;
         }
         if(GUILayout.Button("Back"))
             menu = PlayMenuMode.Home;
     }

     if(menu == PlayMenuMode.Server)
     {
         GUILayout.Label("Port:");
         port = int.Parse(GUILayout.TextArea(port.ToString()));
         if(GUILayout.Button("Start Server")){
             NetworkServer.Listen(port);
             menu = PlayMenuMode.None;
         }

         if(GUILayout.Button("Back"))
             menu = PlayMenuMode.Home;
     }

     if(menu == PlayMenuMode.Connected)
     {
         Transform regionManager = GameObject.Find("_REGIONS").transform;
         for(int i = 0; i < regionManager.childCount; i++)
         {
             if(GUILayout.Button(regionManager.GetChild(i).name))
             {
                 menu = PlayMenuMode.None;
                 SceneManager.LoadSceneAsync(regionManager.GetChild(i).GetComponent<RegionController>().objectSourceScene, LoadSceneMode.Additive);
                 GameObject playerGO = (GameObject)Instantiate(Resources.Load("Player"), Vector3.zero, Quaternion.identity);
                 NetworkServer.AddPlayerForConnection(client.connection, playerGO, playerConnection);
                 playerGO.transform.tag = "Local Player";
                 Camera.main.gameObject.SetActive(false);
                 playerGO.transform.FindChild("Camera").gameObject.SetActive(true);
             }
         }
     }

 }

 void Host()
 {
     NetworkServer.Listen(4855);
     client = ClientScene.ConnectLocalServer();
     client.RegisterHandler(MsgType.Connect, OnConnected);
 }

 void Connect()
 {
     client = new NetworkClient();
     client.RegisterHandler(MsgType.Connect, OnConnected);
     client.Connect(ip, port);
 }

 public void OnConnected(NetworkMessage msg)
 {
     Debug.Log("Connected To Server");
     menu = PlayMenuMode.Connected;
 }

 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerID)
 {
     playerConnection = playerControllerID;
 }

}

I also get these errors: Local invoke: Failed to find local connection to invoke handler on [connectionId=0] for MsgId:12 Local invoke: Failed to find local connection to invoke handler on [connectionId=0] for MsgId:4

And I get these, but i'm pretty sure it's not from the net code: GetLocalizedString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

ArgumentException: GetLocalizedString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. TreeEditor.TreeGroupRoot..cctor ()

Comment
Add comment · Show 1
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 wyl591842446 · Nov 06, 2018 at 03:30 AM 0
Share

I also have this problem and hope someone can answer it. Also, the first run is fine (OnStartHost and OnStopHost), and the second time something goes wrong

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.

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

uNet Connection to IPv6 [Networking] 0 Answers

Unity 5 Multiplayer - Changing When Players Can Connect? 0 Answers

UNET - Keep connection to client when changing scenes 1 Answer

Load scene in unet behind a loading screen 1 Answer

what is wrong here ? 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