- Home /
NullReferenceException
Hey everyone, recently I have been getting these two Errors and I don't know how to solve them,
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.Mesh].get_Item (Int32 index) (at /Applications/buildAgent/work/3df08680c6f85295/mcs/class/corlib/System.Collections.Generic/List.cs:633)
Main.BuildData ()
Main.OnEnable ()
UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
MultiplayerManager:ClientAddPlayerToList(String, NetworkPlayer) (at Assets/Scripts/MultiplayerManager.cs:83)
UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
MultiplayerManager:ServerPlayerJoinRequest(String, NetworkPlayer) (at Assets/Scripts/MultiplayerManager.cs:68)
MultiplayerManager:OnServerInitialized() (at Assets/Scripts/MultiplayerManager.cs:44)
UnityEngine.Network:InitializeServer(Int32, Int32, Boolean)
MultiplayerManager:StartServer(String, String, Int32) (at Assets/Scripts/MultiplayerManager.cs:38)
MenuManager:Main_Host() (at Assets/Scripts/MenuManager.cs:94)
MenuManager:OnGUI() (at Assets/Scripts/MenuManager.cs:33)
and
NullReferenceException
UnityEngine.Mesh.get_bounds () (at C:/BuildAgent/work/a0f11f3559d72c6d/Runtime/ExportGenerated/Editor/Graphics.cs:971)
Main.BuildData ()
Main.OnEnable ()
UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32)
MultiplayerManager:ClientAddPlayerToList(String, NetworkPlayer) (at Assets/Scripts/MultiplayerManager.cs:83)
UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
MultiplayerManager:ServerPlayerJoinRequest(String, NetworkPlayer) (at Assets/Scripts/MultiplayerManager.cs:68)
MultiplayerManager:OnServerInitialized() (at Assets/Scripts/MultiplayerManager.cs:44)
UnityEngine.Network:InitializeServer(Int32, Int32, Boolean)
MultiplayerManager:StartServer(String, String, Int32) (at Assets/Scripts/MultiplayerManager.cs:38)
MenuManager:Main_Host() (at Assets/Scripts/MenuManager.cs:94)
MenuManager:OnGUI() (at Assets/Scripts/MenuManager.cs:33)
Here is the code that contains the instantiate command mentioned in the error.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour
{
public static MultiplayerManager instance;
public string PlayerName;
private string MatchName = "";
private int MaxUsers = 32;
private string MatchPassword = "";
public int altprefix;
public bool MatchStarted = false;
public bool MatchedLoaded = false;
public List<MpPlayer> PlayerList = new List<MpPlayer>();
public List<RoomSettings> RoomSettings = new List<RoomSettings>();
public RoomSettings Settings = null;
public MpPlayer MyPlayer;
public GameObject[] SpawnPoints;
public GameObject PlayerManagerPrefab;
void Start(){
Application.targetFrameRate = 60;
instance = this;
PlayerName = PlayerPrefs.GetString("PlayerName");
Settings = RoomSettings[0];
DontDestroyOnLoad(gameObject);
}
public void StartServer(string Name, string Password, int MaxPlayers){
MatchName = Name;
MatchPassword = Password;
MaxUsers = MaxPlayers;
Network.InitializeServer(MaxPlayers,2550,true);
MasterServer.RegisterHost("TDM",MatchName,"Comment");
//Network.InitializeSecurity();
}
void OnServerInitialized(){
ServerPlayerJoinRequest(PlayerName,Network.player);
}
void OnConnectedToServer(){
networkView.RPC("ServerPlayerJoinRequest",RPCMode.Server,PlayerName,Network.player);
}
void OnPlayerConnected(NetworkPlayer player) {
foreach(MpPlayer pl in PlayerList){
networkView.RPC("ClientAddPlayerToList",player,pl.PlayerName,pl.PlayerNetwork);
}
networkView.RPC("Client_GetMatchSettings",player,Settings.MapLoadName,"","");
}
void OnPlayerDisconnected(NetworkPlayer id){
networkView.RPC("RemovePlayerToList",RPCMode.All,id);
}
void OnDisconnectedFromServer(NetworkDisconnection info) {
PlayerList.Clear();
}
[RPC]
void ServerPlayerJoinRequest(string PlayerName, NetworkPlayer View){
networkView.RPC("ClientAddPlayerToList",RPCMode.All,PlayerName,View);
}
[RPC]
void ClientAddPlayerToList(string PlayerName, NetworkPlayer View){
if(Network.player == View){
MpPlayer tempPlayer = new MpPlayer();
MyPlayer = tempPlayer;
tempPlayer.PlayerName = PlayerName;
tempPlayer.PlayerNetwork = View;
//GameObject Player =
GameObject Player = Network.Instantiate(PlayerManagerPrefab,Vector3.zero,Quaternion.identity,0) as GameObject;
Player.GetComponent<PlayerManager>().thisPlayer = MyPlayer;
PlayerList.Add(tempPlayer);
}
}
[RPC]
void RemovePlayerToList(NetworkPlayer View){
MpPlayer temp = new MpPlayer();
foreach(MpPlayer pl in PlayerList){
if(pl.PlayerNetwork == View){
temp = pl;
}
}
if(temp != null){
PlayerList.Remove(temp);
Network.Destroy(temp.PlayerManager.gameObject);
}
}
[RPC]
void Client_GetMatchSettings(string map, string mode,string others){
Settings = GetMap(map);
}
public RoomSettings GetMap(string name){
RoomSettings get = null;
foreach(RoomSettings rs in RoomSettings){
if(rs.MapLoadName == name){
get = rs;
break;
}
}
return get;
}
[RPC]
void Client_LoadMap(string map, int prefix){
//Network.SetLevelPrefix(prefix);
Application.LoadLevel(map);
}
void OnGUI(){
if(!MyPlayer.PlayerIsAlive && MatchedLoaded)
SpawnMenu();
if(MyPlayer.PlayerIsAlive){
if(GUILayout.Button("Suicide"))
MyPlayer.PlayerManager.HandleBulletDamage(100);
}
}
[RPC]
void Server_SpawnPlayer(NetworkPlayer Player){
int numberSpawn = Random.Range(0,SpawnPoints.Length);
networkView.RPC("Client_SpawnPlayer",RPCMode.All,Player,SpawnPoints[numberSpawn].transform.position,SpawnPoints[numberSpawn].transform.rotation);
}
[RPC]
void Client_SpawnPlayer(NetworkPlayer Player, Vector3 Pos, Quaternion Rot){
MultiplayerManager.GetMPPlayer(Player).PlayerIsAlive = true;
if(Player == MyPlayer.PlayerNetwork){
MyPlayer.PlayerManager.ControllerTranform.position = Pos;
MyPlayer.PlayerManager.ControllerTranform.rotation = Rot;
MyPlayer.PlayerManager.networkView.RPC("Client_ImAlive",RPCMode.All);
// }else{
}
}
void OnLevelWasLoaded(){
MatchedLoaded = true;
if(Application.loadedLevelName == Settings.MapName && Network.isServer){
SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
networkView.RPC("Client_ServerLoaded",RPCMode.AllBuffered);
}
}
[RPC]
void Client_ServerLoaded(){
MatchStarted = true;
}
public static MpPlayer GetMPPlayer(NetworkPlayer Player){
foreach(MpPlayer pl in MultiplayerManager.instance.PlayerList){
if(pl.PlayerNetwork == Player)
return pl;
}
return null;
}
void SpawnMenu(){
if(GUI.Button(new Rect(5,Screen.height-40,250,35),"Spawn"))
{
if(Network.isServer)
Server_SpawnPlayer(Network.player);
else
networkView.RPC("Server_SpawnPlayer",RPCMode.Server,Network.player);
}
}
}
[System.Serializable]
public class MpPlayer {
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
public PlayerManager PlayerManager;
public int PlayerHealth = 100;
public bool PlayerIsAlive;
public int PlayerKills;
public int PlayerDeaths;
}
[System.Serializable]
public class RoomSettings{
public string MapName;
public string MapLoadName;
}
which line is giving you the error? Double click on the error in the console, it'll take you straight to it.
I'd have to dig deeper, but at first read I would advise you to follow convention and not name variables with a capital. A lot in your na$$anonymous$$g is confusing : NetworkPlayer PlayerNetwork??? Why not NetworkPlayer nPl, or networkPlayer, or netPlayer, whatever but stick with it.
Worst, in your class declaration for $$anonymous$$pPlayer, Player$$anonymous$$anager Player$$anonymous$$anager - na$$anonymous$$g a var the same as it's class is asking for trouble.
The only thing in a class declaration which SHOULD have the same name as the class is it's constructor.
Never $$anonymous$$d, sorry for wasting your time, but I fixed it, for some reason loads of the mesh objects in the playerobject had scripts attached to them, and apparently I deleted the script that was attached and was causing me those errors.. But I'll keep the advice you gave me in $$anonymous$$d :D Thank you!!!!!!!!!!!!!!
Can you reply and accept your own answer then? Helps keeping things tidy (your question wont be marked as unanswered anymore).
Your answer
![](https://koobas.hobune.stream/wayback/20220613081903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
MeshCombineUtility -1 Answers
combining children error 2 Answers
Cant convert MeshFilter to Mesh 1 Answer