Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Having trouble getting everything to talk to each other just right. Please help.

Discussion in 'Multiplayer' started by tylerreece22, Nov 12, 2015.

  1. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    I am using a WAMP server as my master server and I start the server window by building and running (which I am not sure if I have to do because my WAMP server is continuously running the background of my computer) then I connect to it using the Network.Connection("127.0.0.1", 23466); and it seems like I have connected to the server just fine but my game at that point doesnt turn into a client like the tutorial I am following says it should.

    CORRECTION: When I run the Network.TestConnection(); it says Timeout during connection test

    Two questions:
    1. Do I actually have to run a server through WAMP in Unity? If so, why?
    2. Why doesnt my game turn into a client?

    Thank you so much for the help ahead of time!


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NetworkManager : MonoBehaviour {
    5.  
    6.     string registeredGameName = "Test MMO Main Server";
    7.     bool isRefreshing = false;
    8.     float refreshRequestLength = 3.0f;
    9.     HostData[] hostData;
    10.  
    11.     public void OnGUI() {
    12.  
    13.         if (Network.isClient || Network.isServer)
    14.             return;
    15.  
    16.  
    17.         if (GUI.Button (new Rect (25f, 25f, 150f, 30f), "Start Server")) {
    18.             //start server function here
    19.             StartServer();
    20.         }
    21.  
    22.         if (GUI.Button (new Rect (25f, 75f, 150f, 30f), "Refresh Server List")) {
    23.             //refresh server list function here
    24.             StartCoroutine("RefreshHostList");
    25.         }
    26.         //connects to server
    27.         if (GUI.Button (new Rect (25f, 125f, 150f, 30f), "Connect to server")) {
    28.             Network.Connect ("127.0.0.1", 23466);
    29.             Debug.Log ("Connection ip is " + Network.player.ipAddress);
    30.             Network.TestConnection();
    31.             if (Network.isClient){
    32.                 Debug.Log ("You are now a client");
    33.             } else if (Network.isServer) {
    34.                 Debug.Log ("You are now a server");
    35.             }
    36.         }
    37.         //also only works for shared hosting
    38.         //displays buttons for each available host server and when you click them it connects
    39.         if (hostData != null) {
    40.             for(int i = 0; i < hostData.Length; i++) {
    41.                 if(GUI.Button(new Rect(Screen.width / 2, 65f + (30f * i), 300f, 30f), hostData[i].gameName)) {
    42.                     Network.Connect (hostData[i]);
    43.                 }
    44.             }
    45.         }
    46.     }
    47.  
    48.     private void ResetIP() {
    49.         MasterServer.ipAddress = "127.0.0.1";
    50.         MasterServer.port = 8000;
    51.         Network.natFacilitatorIP = "127.0.0.1";
    52.         Network.natFacilitatorPort = 50005;
    53.     }
    54.  
    55.     private void StartServer(){
    56.         ResetIP ();
    57.         Network.InitializeServer (16, 23466, false);
    58.         //MasterServer.RegisterHost (registeredGameName, "Networking test", "Test Implementation of Server Code");
    59.         //Do I need to register a host considering this is not a shared hosting server?
    60.     }
    61.     //would i only need this method if i needed to register a host on the shared hosting server?
    62.     void OnMasterServerEvent(MasterServerEvent masterServerEvent) {
    63.         if (masterServerEvent == MasterServerEvent.RegistrationSucceeded) {
    64.             Debug.Log ("Registration Successful!");
    65.         } else {
    66.             Debug.Log ("Error: Could not register.");
    67.         }
    68.     }
    69.     //I believe this method is also only effective if I am connected to a shared hosting server
    70.     public IEnumerator RefreshHostList() {
    71.         Debug.Log ("Refreshing...");
    72.         MasterServer.RequestHostList (registeredGameName);
    73.         float timeStarted = Time.time;
    74.         float timeEnd = Time.time + refreshRequestLength;
    75.      
    76.         while (Time.time < timeEnd) {
    77.             hostData = MasterServer.PollHostList ();
    78.             yield return new WaitForEndOfFrame();
    79.         }
    80.         if (hostData == null || hostData.Length == 0) {
    81.             Debug.Log ("No active servers found.");
    82.         } else {
    83.             Debug.Log(hostData.Length + " have been found.");
    84.         }
    85.     }
    86.  
    87.     private void SpawnPlayer() {
    88.         Debug.Log ("Spawning Player...");
    89.         //Network.Instantiate (Resources.Load ("Prefabs/Player"), new Vector3 (0f, 2, 5f, 0f), Quaternion.identity, 0);
    90.     }
    91.  
    92.     void OnServerInitialized() {
    93.         Debug.Log ("Server has been initialized");
    94.         //SpawnPlayer ();
    95.     }
    96. }
    97.  
     
  2. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    @Carrel I saw your post on another question and tried what you said and unfortunately I couldnt get my client to work. Please help.