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

Need help with Network script

Discussion in 'Multiplayer' started by fredr92, Jun 4, 2014.

  1. fredr92

    fredr92

    Joined:
    Jul 8, 2013
    Posts:
    292
    hI
    Im getting an error in my network script.
    Would really appreciate if someone could help me finding out what i have done wrong.
    I have looked over it for days now.

    This is the error

    Assets/Scripts/MultiplayerManager.cs(127,10): error CS0116: A namespace can only contain types and namespace declarations

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. public class MultiplayerManager : MonoBehaviour
    6. {
    7.  public static MultiplayerManager instance;
    8.  public string PlayerName;
    9.  private string MatchName = "";
    10.  private string MatchPassword = "";
    11.  private int MatchMaxUsers = 32;
    12.  public List<MPPlayer> PlayerList = new List<MPPlayer>();
    13.  public List<MapSetting> MapList = new List<MapSetting>();
    14.  public MapSetting CurrentMap = null;
    15.  public int oldprefix;
    16.  public bool IsMatchStarted = false;
    17.  void start()
    18.  {
    19.  instance = this;
    20.  PlayerName = PlayerPrefs.GetString("PlayerName");  
    21.  CurrentMap = MapList[0];
    22.  }
    23.  void FixedUpdate()
    24.  {
    25.  instance = this;
    26.  }
    27.  public void StartServer(string servername, string serverpassword, int maxusers)
    28.  {
    29.  MatchName = servername;
    30.  MatchPassword = serverpassword;
    31.  MatchMaxUsers = maxusers;
    32.  Network.InitializeServer(MatchMaxUsers, 2550, false);  
    33.  MasterServer.RegisterHost("DeathMatch", MatchName, "");
    34.  //Network.InitializeSecurity();
    35.  }
    36.  void OnServerInitialized()  
    37.  {
    38.  Server_PlayerJoinRequest(PlayerName, Network.player);
    39.  }
    40.  void OnConnectedToServer()  
    41.  {
    42.  networkView.RPC("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
    43.   }
    44.  void OnPlayerDisconnected(NetworkPlayer id)
    45.  {
    46.  networkView.RPC("Client_RemovePlayer", RPCMode.All, id);
    47.  }
    48.  void OnPlayerConnected(NetworkPlayer player)
    49.  {
    50.  foreach(MPPlayer p1 in PlayerList)
    51.  {
    52.  networkView.RPC("Client_AddPlayerToList", player, p1.PlayerName, p1.PlayerNetwork);
    53.  }
    54.   networkView.RPC("Client_GetMultiplayerMatchSettings", player, CurrentMap.MapName, "", "");
    55.  }
    56.  [RPC]
    57.  void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
    58.  {
    59.  networkView.RPC("Client_AddPlayerToList", RPCMode.All, playername, view);
    60.  }
    61.  [RPC]
    62.  void Client_AddPlayerToList(string playername, NetworkPlayer view)
    63.  {
    64.  MPPlayer tempplayer = new MPPlayer();
    65.  tempplayer.PlayerName = playername;
    66.  tempplayer.PlayerNetwork = view;
    67.  PlayerList.Add(tempplayer);
    68.  }
    69.  [RPC]
    70.  void Client_RemovePlayer(NetworkPlayer view)
    71.  {
    72.  MPPlayer temppl = null;
    73.  foreach(MPPlayer p1 in PlayerList)
    74.  {
    75.  if (p1.PlayerNetwork == view)
    76.  {
    77.  temppl = p1;  
    78.  }
    79.  }
    80.  if (temppl != null) {  
    81.  PlayerList.Remove(temppl);  
    82.   }
    83.   }
    84.  [RPC]
    85.  void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
    86.  {
    87.  CurrentMap = GetMap(map);
    88.  }
    89.  public  MapSetting GetMap(string name)
    90.  {
    91.  MapSetting get = null;
    92.  foreach(MapSetting st in MapList)
    93.   {
    94.  if (st.MapName == name)
    95.  {
    96.  get = st;
    97.  break;
    98.  }
    99.  }
    100.  return get;
    101.  }
    102. }
    103.  
    104.  [RPC]
    105.   void Client_LoadMultiplayerMap(string Map, int prefix)
    106.   {
    107.  Network.SetLevelPrefix(prefix);
    108.  Application.LoadLevel(map);
    109.   }
    110.  
    111. [System.Serializable]  
    112.   public class MPPlayer
    113.   {
    114.  public string PlayerName = "";
    115.  public NetworkPlayer PlayerNetwork;  
    116.   }
    117. [System.Serializable]
    118. public class MapSetting
    119.   {
    120.  public string MapName;
    121.  public string MapLoadName;
    122.  public Texture MapLoadTexture;
    123.   }
    124. [CODE/]
     
  2. fredr92

    fredr92

    Joined:
    Jul 8, 2013
    Posts:
    292
    Something strange happens on forum when i use a line for Space it dissapear in my post so there are null Space lines. so sorry for that . cause the error says line 127,10, but because the Space lines have dissapeared its fewer lines. no idea why. Hope it still can be possible too se what wrong
     
  3. fredr92

    fredr92

    Joined:
    Jul 8, 2013
    Posts:
    292
    This is Line 127 void Client_LoadMultiplayerMap(string Map, int prefix)
     
  4. nastasache

    nastasache

    Joined:
    Jan 2, 2012
    Posts:
    74
    You have all the code starting with [RPC] void Client_LoadMultiplayerMap(string Map, int prefix), outside of class code.
     
  5. fredr92

    fredr92

    Joined:
    Jul 8, 2013
    Posts:
    292
    So i removed a bracket } after return so im now using only 1 instead off 2 . Is that what u meant ? Or can You show with example please
     
  6. matheuslr

    matheuslr

    Joined:
    Aug 27, 2013
    Posts:
    42
    Just put the method "void Client_LoadMultiplayerMap(string Map, int prefix) { ... }" inside your MultiplayerManager class!