- Home /
Argument out of Range
I'm trying to build a multiplayer FPS for my class. I'm using this tutorial: http://www.youtube.com/watch?v=lFI4FGWL-fs
I got all the way to the end, I believe I've followed it to the nose, but when I go to start my match, it takes me back to the main menu and gives me the error "ArgumentOutOfRangeException: Argument is out of range. Parameter name: Index" It's referencing line 26 of my code which is "CurrentMap = MapList[0];"
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
public static MultiplayerManager instance;
public string PlayerName;
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public List<MapSetting> MapList = new List<MapSetting>();
public MapSetting CurrentMap = null;
public int oldprefix;
public bool IsMatchStarted = false;
void Start()
{
instance = this;
PlayerName = PlayerPrefs.GetString("PlayerName");
CurrentMap = MapList[0];
}
void FixedUpdate()
{
instance = this;
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer(MatchMaxUsers, 2550, false);
MasterServer.RegisterHost("Death Match", MatchName, "");
//Network.InitializeSecurity();
}
void OnServerInitialized()
{
Server_PlayerJoinRequest(PlayerName, Network.player);
}
void OnConnectedToServer()
{
networkView.RPC("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
}
void OnPlayerDisconnected(NetworkPlayer id)
{
networkView.RPC("Client_RemovePlayer", RPCMode.All, id);
}
void OnPlayerConnected(NetworkPlayer player)
{
foreach(MPPlayer pl in PlayerList)
{
networkView.RPC("Client_AddPlayerToList", player, pl.PlayerName, pl.PlayerNetwork);
}
networkView.RPC("Client_GetMultiplayerMatchSettings", player, CurrentMap.MapName, "", "");
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
networkView.RPC("Client_AddPlayerToList", RPCMode.All, playername, view);
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add(tempplayer);
}
[RPC]
void Client_RemovePlayer(NetworkPlayer view)
{
MPPlayer temppl = null;
foreach(MPPlayer pl in PlayerList)
{
if(pl.PlayerNetwork == view)
{
temppl = pl;
}
}
if(temppl != null)
{
PlayerList.Remove(temppl);
}
}
[RPC]
void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
{
CurrentMap = GetMap(map);
}
public MapSetting GetMap(string name)
{
MapSetting get = null;
foreach(MapSetting st in MapList)
{
if(st.MapName == name)
{
get = st;
break;
}
}
return get;
}
[RPC]
void Client_LoadMultiplayerMap(string map, int prefix)
{
Network.SetLevelPrefix(prefix);
Application.LoadLevel(map);
}
}
[System.Serializable]
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
}
[System.Serializable]
public class MapSetting
{
public string MapName;
public string MapLoadName;
public Texture MapLoadTexture;
}
I've been told that I need to assign a value to my MapList, but I'm not sure where or how to do that. Any help would be greatly appreciated.
Answer by CodeMasterMike · Dec 04, 2012 at 06:20 AM
Your MapList-variable is a list, which should contain a MapSetting class instance.
So this error message means that the element in the list, in your case element 0, is empty. Meaning the whole list is empty. So you can't load a map.
So you need to fill this list with MapSetting variables before you can call it. Reading the code, you should somewhere before you make the "CurrentMap = MapList[0];", add at least one map to the list.
You probably need to add some MapSetting variables through the script inspector in the editor.
Good luck!
The scene I was using for my map apparently wasn't good enough (it only had a terrain and nothing else done to it). I got a proper one from the asset store and it fixed my list problem.
Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
FPS SCRIPT ERROR PLEASE HELP 2 Answers
Multiplayer FPS Tutorial 1 Answer
what is wrong with this online FPS script(not done) ? 1 Answer
error CS1061 anybody might help me? 1 Answer