Search Unity

Connecting twice problem

Discussion in 'Editor & General Support' started by Thesaurus1233, May 6, 2014.

  1. Thesaurus1233

    Thesaurus1233

    Joined:
    Apr 5, 2014
    Posts:
    5
    Hi, i have a problem with unity networking, but because a don't know how to explain it i filmed a video



    Here is the script

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class NetworkManager : MonoBehaviour {
    6.     public string PlayerName;
    7.     public string MatchName;
    8.     public static NetworkManager Instance;
    9.     public List<Player> PlayerList = new List<Player>();
    10.     public Player MyPlayer;
    11.     // Use this for initialization
    12.  
    13.     void Awake(){
    14.         Instance = this;
    15.     }
    16.  
    17.     void Start () {
    18.         DontDestroyOnLoad(gameObject);
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.         PlayerName = PlayerPrefs.GetString("PlayerName");
    24.     }
    25.  
    26.     public void StartServer(string ServerName, int MaxPlayers){
    27.         Network.InitializeSecurity();
    28.         Network.InitializeServer(MaxPlayers, 25565, true);
    29.         MasterServer.RegisterHost("FPsMulty",ServerName, "");
    30.         Debug.Log("Se empezo el server");
    31.     }
    32.     void OnPlayerConnected(NetworkPlayer id){
    33.         networkView.RPC("Server_PlayerJoined",RPCMode.Server,PlayerName,id);
    34.         foreach(Player pl in PlayerList){
    35.             networkView.RPC("Client_PlayerJoined",id,pl.PlayerName,pl.OnlinePlayer);
    36.         }
    37.     }
    38.  
    39.     void OnServerInitialized(){
    40.         Server_PlayerJoined(PlayerName,Network.player);
    41.     }
    42.  
    43.     void OnConnectedToServer(){
    44.         networkView.RPC("Server_PlayerJoined",RPCMode.Server, PlayerName, Network.player);
    45.     }
    46.  
    47.     void OnPlayerDisconnected(NetworkPlayer id){
    48.         networkView.RPC("RemovePlayer", RPCMode.All, id);
    49.         Network.RemoveRPCs(id);
    50.     }
    51.  
    52.     void OnDisconnectedFromServer(NetworkDisconnection info){
    53.             PlayerList.Clear();
    54.     }
    55.        
    56.     [RPC]
    57.     public void Server_PlayerJoined(string Username, NetworkPlayer id){
    58.         networkView.RPC("Client_PlayerJoined",RPCMode.All, Username, id);
    59.     }
    60.     [RPC]
    61.     public void Client_PlayerJoined(string Username, NetworkPlayer id){
    62.         Player temp = new Player();
    63.         temp.PlayerName = Username;
    64.         temp.OnlinePlayer = id;
    65.         PlayerList.Add(temp);
    66.         if(Network.player == id){
    67.             MyPlayer = temp;
    68.         }
    69.     }
    70.     [RPC]
    71.     public void RemovePlayer(NetworkPlayer id){
    72.         Player temp = new Player();
    73.         foreach(Player pl in PlayerList){
    74.             if(pl.OnlinePlayer == id){
    75.                 temp = pl;
    76.             }
    77.         }
    78.         if(temp != null){
    79.             PlayerList.Remove(temp);
    80.         }
    81.     }
    82. }
    83.  
    84. [System.Serializable]
    85.     public class Player{
    86.     public string PlayerName;
    87.     public NetworkPlayer OnlinePlayer;
    88. }
    89.  
    and

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Menu : MonoBehaviour {
    5.  
    6.     public Menu  instance;
    7.     private string CurMenu;
    8.     public string Nombre;
    9.     public string MatchName = "Name";
    10.     public int Players;
    11.     // Use this for initialization
    12.  
    13.     void Start () {
    14.         CurMenu = "Main";
    15.         instance = this;
    16.         Nombre = PlayerPrefs.GetString("PlayerName");
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.     }
    23.  
    24.     void ToMenu(string menu){
    25.         CurMenu = menu;
    26.     }
    27.  
    28.     void OnGUI(){
    29.         if(CurMenu == "Main")
    30.             Main();
    31.         if(CurMenu == "Host")
    32.             Host();
    33.         if(CurMenu == "Lobby")
    34.             Lobby();
    35.         if(CurMenu == "List")
    36.             MatchList();
    37.     }
    38.  
    39.     private void Main(){
    40.         if (GUI.Button(new Rect(0,0,128,32),"Crea una partida")){
    41.             ToMenu("Host");
    42.         }
    43.         Nombre = GUI.TextField(new Rect(130,0,128,32),Nombre);
    44.         if (GUI.Button(new Rect(260,0,128,32),"Guardar")){
    45.             PlayerPrefs.SetString("PlayerName", Nombre);
    46.         }
    47.         if (GUI.Button(new Rect(0,33,128,32),"Lista de Servidores")){
    48.             ToMenu("List");
    49.         }
    50.    
    51.   }
    52.  
    53.     private void Host(){
    54.         if (GUI.Button(new Rect(0,0,128,32),"Empezar")){
    55.             NetworkManager.Instance.StartServer(MatchName, Players);
    56.             ToMenu("Lobby");
    57.         }
    58.  
    59.         if (GUI.Button(new Rect(0,33,128,32),"Atras")){
    60.             ToMenu("Main");
    61.         }
    62.             MatchName = GUI.TextField(new Rect(130,0,128,32),MatchName);
    63.             GUI.Label(new Rect(260,0,128,32),"Nombre de la partida");
    64.             Players =Mathf.Clamp(Players,0,32);
    65.             GUI.Label(new Rect(136,33,128,32),"Jugadores Máximos");
    66.             if (GUI.Button(new Rect(265,33,32,32),"+"))
    67.                 Players ++;
    68.         GUI.Label(new Rect(320,33,64,32),Players.ToString());
    69.         if (GUI.Button(new Rect(345,33,32,32),"-"))
    70.             Players --;
    71.     }
    72.  
    73.     private void Lobby(){
    74.         if(Network.isServer){
    75.         if (GUI.Button(new Rect(Screen.width - 128,Screen.height - 64,128,32),"Empezar")){
    76.            
    77.         }
    78.         }
    79.        
    80.         if (GUI.Button(new Rect(Screen.width - 128,Screen.height - 32,128,32),"Atras")){
    81.             ToMenu("Host");
    82.         }
    83.  
    84.         GUILayout.BeginArea(new Rect(0,0,Screen.width/2,Screen.height));
    85.         foreach(Player pl in NetworkManager.Instance.PlayerList){
    86.             GUILayout.Label(pl.PlayerName);
    87.         }
    88.         GUILayout.EndArea();
    89.     }
    90.  
    91.     private void MatchList(){
    92.         if (GUI.Button(new Rect(0,0,128,32),"Refrescar")){
    93.             MasterServer.RequestHostList("FPsMulty");
    94.         }
    95.         if (GUI.Button(new Rect(0,33,128,32),"Atras")){
    96.             ToMenu("Main");
    97.         }
    98.  
    99.         GUILayout.BeginArea(new Rect(Screen.width / 2,0,Screen.width / 2,Screen.height),"Lista de Servidores","box");
    100.         foreach(HostData hd in MasterServer.PollHostList()){
    101.             GUILayout.BeginHorizontal();
    102.             GUILayout.Label(hd.gameName);
    103.             if (GUILayout.Button("Conectar")){
    104.                 Network.Connect(hd);
    105.                 ToMenu("Lobby");
    106.             }
    107.             GUILayout.EndHorizontal();
    108.         }
    109.         GUILayout.EndArea();
    110.     }
    111. }
    112.  
    If someone could help I would be very grateful
     
  2. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    Well, you posted a private video...
     
  3. Crichton333

    Crichton333

    Joined:
    May 4, 2014
    Posts:
    113
    Please dont be a video filmed with a phone.
     
  4. Thesaurus1233

    Thesaurus1233

    Joined:
    Apr 5, 2014
    Posts:
    5
    Just fix it, sorry
     
  5. Thesaurus1233

    Thesaurus1233

    Joined:
    Apr 5, 2014
    Posts:
    5
    Just fix it, sorry for that