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 Crilled · Jul 08, 2013 at 04:57 PM · networkserverclientconnect

Why won't my network code run on LAN?

Here's my network code, I can connect on the same machine but when I try two separate computers it won't connect.

 var ServerIP: String = "127.0.0.1";
 var ServerPort: String = "30000";
 
 function OnGUI(){
 if (Network.peerType == NetworkPeerType.Disconnected){
     if (GUILayout.Button("Connect")){
         Network.Connect(ServerIP, parseInt(ServerPort));
         
         }
     if (GUILayout.Button("New Server")){
         Network.InitializeServer(32, parseInt(ServerPort), false);    
         }
     }else{
      if (GUILayout.Button("Disconnect")){
          Network.Disconnect();
      }
     }
 }
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
2
Best Answer

Answer by PProductions · Jul 08, 2013 at 05:06 PM

The ip address 127.0.0.1 is connecting to the same machine, to connect to a different machine you need that machine's ip address. If the other computer is on your internet network(e.g. connected to your wifi) then you can connect using it's local ip. If it is not then you will need to port forward the global ip through your firewall so you can host a server on it. I would suggest looking up some youtube videos on minecraft server setups as I find they help explain port forwarding.

However, if I were you I would be using unity's master server functionality. It is much more advanced and in this example you don't need to worry about knowing the server's ip directly.

Some useful links:

  • http://portforward.com/

  • http://www.youtube.com/user/steamisM50/search?query=multiplayer

  • http://docs.unity3d.com/Documentation/ScriptReference/MasterServer.html

I hope this helps

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
avatar image
0

Answer by mor0022 · Apr 15, 2014 at 06:57 AM

Here is my Code, Still trying to work out how to destroy objects neatly.

 using UnityEngine;
 using System.Collections;
 
 public class LanNetworkManager : MonoBehaviour {
 
     public string gameName = "not needed for lan";
     public string hostGameName = "Tutorial Networking";
     public GameObject playerPrefab;
     public Transform spawnObject;
     public int maxPlayers = 6;
 
     private string ip = "127.0.0.1";
     private int playerCount = 0;
     
     private bool gameStarted = false;
 
     private int btnX;
     private int btnY;
     private int btnW;
     private int btnH;
 
     void Start(){
         btnX = 10;
         btnY = 10;
         btnW = 130;
         btnH = 20;
     }
 
     void startServer(){
         bool useNat = false; //!Network.HavePublicAddress ();
         Network.InitializeServer (maxPlayers, 25017,useNat);
     }
 
 
     void OnServerInitialized(){
         Debug.Log ("Server Started");
         spawnPlayer ();
     }
 
     void OnConnectedToServer(){
         Debug.Log ("Server Joined");
         spawnPlayer ();
     }
 
     void ConnectToLanServer(string ipA){
         Network.Connect (ipA, 25017);
     }
 
     void OnPlayerConnected(){
         playerCount++;
         Debug.Log ("Player Connected");
     }
 
     void OnPlayerDiconnected(){
         playerCount--;
         Debug.Log ("Player Diconnected");
     }
 
 
     
 
     void OnGUI(){
         if (!Network.isClient && !Network.isServer && !gameStarted) {
             if (GUI.Button (new Rect (btnX, btnY, btnW, btnH), "Start Server")) {
                 gameStarted = true;
                 startServer ();
             }
             ip = GUI.TextField (new Rect (btnX, btnY * 4, btnW, btnH), "127.0.0.1");
             if (GUI.Button (new Rect (btnX, btnY * 7, btnW, btnH), "Manual Connect")) {
                 gameStarted = true;
                 ConnectToLanServer (ip);
             }
         } else {
             if(GUI.Button (new Rect(btnX,btnY*11,btnW,btnH),"Disconnect")){
                 gameStarted = false;
                 //Network.DestroyPlayerObjects(Network.player);
                 Network.Disconnect(500);
             }
         }
         if (Network.isServer) {
             GUI.Label (new Rect(btnX,btnY*15,btnW,btnH),"Players: " + (playerCount + 1) + "/" + maxPlayers);
         }
     }
 
 
 
 
     void PlayerSpawned(){
         Debug.Log ("Player Spawned");
     }
 
     void spawnPlayer(){
         Network.Instantiate (playerPrefab, spawnObject.position, Quaternion.identity, 0);
         PlayerSpawned ();
     }
     
 
 }
 
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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

connect with Server(Host) 0 Answers

Can anyone help me with my network script? 1 Answer

Download pictures and store them temporarily 1 Answer

ClientScene.AddPlayer resulting in 'Unknown message ID' error 1 Answer

Can you tell OnSerializeNetworkView who the owner is? 3 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