- Home /
helpme RPC
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour
{
private string MSName = "SurvivorV2020_16123";
private HostData[] hostData;
public GameObject[] Maps;
public GameObject[] Characters;
public static GameObject myPlayer;
private int map;
private int character;
public static string Name = "";
public List<string> Jogadores = new List<string>();
//Administraçao de salas
void NovaSala()
{
Network.InitializeServer(16, 21022, !Network.HavePublicAddress());
MasterServer.RegisterHost(MSName, "ServerTest");
Debug.Log("Server Criado.");
}
void Actualizar()
{
MasterServer.RequestHostList(MSName);
hostData = MasterServer.PollHostList();
}
//Spawn de mapas e jogadores
void Spawn(int mapa, int jogador)
{
myPlayer = Network.Instantiate(Characters[jogador], new Vector3(0, 2, 0), Quaternion.identity, 0) as GameObject;
Instantiate(Maps[mapa], new Vector3(0, 0, 0), Quaternion.identity);
}
//Eventos
void OnConnectedToServer()
{
//Spawn(0, 0);
networkView.RPC("addPlayer", RPCMode.AllBuffered, Name);
}
void OnServerInitialized()
{
//Spawn(0, 0);
networkView.RPC("addPlayer", RPCMode.AllBuffered, Name);
}
void OnDisconnectedFromServer()
{
Network.Destroy(myPlayer);
}
//UI do jogo
void OnGUI()
{
if (Network.isClient || Network.isServer)
{
Lobby();
return;
}
Actualizar();
GUILayout.BeginArea(new Rect(5, 5, Screen.width - 10, Screen.height - 10), "", "box");
GUILayout.BeginHorizontal();
{
if (GUILayout.Button("Nova Sala"))
NovaSala();
Name = GUILayout.TextField(Name, GUILayout.Width(200));
if (GUILayout.Button("Aplicar"))
{
networkView.name = Name;
}
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal("box");
{
if (hostData != null)
{
foreach (HostData sala in hostData)
{
GUILayout.Label(sala.gameName + sala.connectedPlayers);
if (GUILayout.Button("Connect"))
Network.Connect(sala);
}
}
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
void Lobby()
{
if (PlayerController.isSpawned) return;
GUILayout.BeginArea(new Rect(5, 5, (Screen.width / 2) - 5, Screen.height - 10), "", "box");
{
foreach(string jogador in Jogadores)
{
GUILayout.Label(jogador);
}
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect((Screen.width / 2), 5, (Screen.width / 2) - 5, Screen.height - 10), "", "box");
{
if(GUILayout.Button("Spawn"))
{
Spawn(0, 0);
}
}
GUILayout.EndArea();
}
[RPC]
void addPlayer(string name)
{
Jogadores.Add(name);
networkView.name = name;
}
}
ERROR IndexOutOfRangeException: Array index is out of range. fn_NetConfig.Spawn (Int32 mapa, Int32 jogador) (at Assets/ProjetoDefinitivo/Scripts/NetWork/fn_NetConfig.cs:37)
Answer by robertbu · Aug 03, 2014 at 12:30 AM
If the error message lines up with the code above it looks like this is causing your error:
Characters[jogador]
Since I only see one call to Spawn (line 109), and it passes 0 for jogador, I'm guessing that the Characters array does not have anything assigned. Typically with public variables, the array should be assigned through drag and drop in the Inspector.
yes not have errors I not have placed the script in character
thanks man
Your answer
Follow this Question
Related Questions
Array index is out of Range!? 1 Answer
Wierd Animation Bug 0 Answers
C# - IndexOutOfRangeException - Array index is out of range 2 Answers
IndexOutOfRangeException 1 Answer
Random object, Array index out of range 0 Answers