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 LewiksR · Jun 16, 2015 at 05:44 AM · c#unity 5networkingconnectlan

How to Connect() in LAN, not working?

I'm using the default Connect function to do my networking, the server creation seems to work fine, but when i try to connect from another computer in the LAN or even the very host's computer (using another window running the same build of the game) it does not connect to the host. I have used the same code in Unity 4.5/4.6 and it did work, even connect to the same computer, or even a Hamachi network.

I wasn't able to test this with a computer outside my LAN, but even if i can connect that way, i want LAN connection to give players more options, like playing with a friend without having to pay for a dedicated server or joining a server with tons of people if they don't want the social interaction. (like me when i play)

Also, i did try and open my modem's ports, but nothing changed.

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
 
     public GameObject human;
 
     string ip = "xxx.xxx.xxx.xxx";
     string connectedPlayers;
 
     void Update () { // this edits the string that shows in a side panel, for debugging
         connectedPlayers = Network.player.ipAddress;
 
         foreach (NetworkPlayer p in Network.connections) {
             connectedPlayers = "\n" + connectedPlayers + p.ipAddress;
         }
     }
 
     void SpawnMyself () {
         if (human != null) {
             GameObject me = (GameObject)Network.Instantiate (human, Vector3.zero, Quaternion.identity, 0);
             me.GetComponent<PlayerController> ().enabled = true;
             me.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(4).GetChild(0).GetChild(0).GetChild(0).gameObject.SetActive(true);
         } else {
             Debug.LogError("No Human GameObject is set in " + transform.name);
         }
     }
 
     void OnGUI () {
         if (!(Network.isClient || Network.isServer)) {
             ip = GUI.TextField (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f - 40, 200, 20), ip);
 
             if (GUI.Button (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f - 10, 200, 20), "Connect")) {
                 ConnectToServer ();
             }
 
             if (GUI.Button (new Rect (Screen.width * 0.5f - 100, Screen.height * 0.5f + 20, 200, 20), "Host server")) {
                 StartServer ();
             }
 
             GUI.TextArea (new Rect (0, 0, 200, Screen.height), connectedPlayers);
         }
     }
 
     void ConnectToServer () {
         Network.Connect(ip, 26000);
     }
 
     void StartServer () {
         bool useNat = !Network.HavePublicAddress();
         Network.InitializeServer(32, 26000, useNat);
     }
 
     void OnConnectedToServer () {
         ip = "connected!";
         SpawnMyself ();
     }
 
     void OnServerInitialized () {
         ip = "created server!";
         SpawnMyself ();
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KdRWaylander · Jun 16, 2015 at 07:27 AM

Hi,

What ip adress have you tried ? Make sur to check that ip using "ipconfig -all"

Here is a script i use to connect locally on my network, it tries to connect first on the same computer "127.0.0.1" and then if it doesn't succeed, on the network

 using UnityEngine;
 using System.Collections;
 
 public class ClientProps : MonoBehaviour {
     private string serverIPLocal = "127.0.0.1";
     private string serverIPRemote = "10.221.34.210";
     private int serverPort = 25000;
     
     // Use this for initialization
     void Start () {
         Connexion ();
     }
 
     private void Connexion () {
         if (Network.peerType == NetworkPeerType.Disconnected) {
             Network.Connect(serverIPLocal, serverPort);
         }
         
         if (Network.peerType == NetworkPeerType.Disconnected) {
             Network.Connect(serverIPRemote, serverPort);
         }
     }
 
     /* MESSAGES */
     void OnConnectedToServer () {
         // stuff
     }
 }

It may sound crazy but try passing only variables in your Connect and InitializeServer methods, not direct values. And check the ip ^^

Comment
Add comment · Show 4 · 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 LewiksR · Jun 16, 2015 at 03:08 PM 0
Share

I tried to pass only variables, but to no help. It's not the IP, as i have it show in the side panel which is active while you are prompted to either connect or host just after the game is launched. I tried every "192.168.0.x" from 0 to 9 and "127.0.0.1", even leaving the IP as "localhost", it keeps throwing the same error in the editor "The connection request to ip:port failed. Are you sure the server can be connected to?". I'm quite a begginer when it comes to networking (and mostly everything else lol) so i have no idea why it doesn't work now, since it did before Unity 5.0. Also, i've updated to 5.1 with the "new networking", but i'm quite afraid to use their scripts, both due of the lack of documentation and the fact that i like to know exactly what the hell is going on in the scripts i use (i also like to model my own stuff mostly, i don't trust other people enough for that hehe)

avatar image KdRWaylander · Jun 17, 2015 at 12:00 PM 0
Share

$$anonymous$$mmmm usually when i get this error is when i forgot to launch my server. You do have the debug log saying "created server!" ?

avatar image LewiksR · Jun 17, 2015 at 01:46 PM 0
Share

Yes, as you can see in my code, the player only spawns when the server initiates, so it's pretty obvious it started succesfully.

avatar image KdRWaylander · Jun 17, 2015 at 02:31 PM 0
Share

Try using two different strings: the 'ip' string you use to store "127.0.0.1" or else, and the one you use to display "connected" or "created server !" ?

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

2 People are following this question.

avatar image avatar image

Related Questions

Unet LAN Connection Function 0 Answers

UNet equivalent of OnDisconnectedFromServer 0 Answers

Commands and ClientRPC are not running when called 0 Answers

Multiple Cars not working 1 Answer

Photon wont sync for the masterclient. 1 Answer


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