- Home /
Problem with spawn player with RPC calls(c #)
Hi, im working on adventure game in multiplayer, and i made the code for spawn, but im stuck in one point, im show you the situation:
this piece of code is attached of a gameObject(DontDestroyOnLoad already placed)
bool _isSpawned = false;
public GameObject player;
public List<MPPlayer> playerList = new List<MPPlayer>();
void OnLevelWasLoaded(int level)
{
if (level == 1)
{
if (!_isSpawned)
{
spawnPoint = GameObject.FindGameObjectsWithTag("spawnpoint");
networkView.RPC("Server_AssignSpawnPoint", RPCMode.All, Network.player);
_isSpawned = true;
}
}
[RPC]
public void Server_AssignSpawnPoint(NetworkPlayer playerNet)
{
if (Network.isServer)
{
for (int i = 0; i < playerList.Count; i++)
{
if (playerList[i].plNetwork == playerNet)
{
networkView.RPC("Client_SpawnPlayer", playerNet, i);
break;
}
}
}
}
[RPC]
public void Client_SpawnPlayer(int index)
{
GameObject playerNet = Network.Instantiate(player, spawnPoint[index].transform.position, spawnPoint[index].transform.rotation, 0) as GameObject;
}
public class MPPlayer
{
public string plName = "";
public NetworkPlayer plNetwork;
}
This is level0 scene and when i load new level(level1) this code is called at start, in this code above there is this piece of code:
for (int i = 0; i < playerList.Count; i++)
{
if (playerList[i].plNetwork == playerNet)
{
networkView.RPC("Client_SpawnPlayer", playerNet, i);
break;
}
}
i want that the client(or server) in list call method and spawn own player character, i have specific that who must call the function is Network.player(playerNet), but when i try to spawn the console send me this message:
Can't send RPC function because the target is not connected to the server. UnityEngine.NetworkView:RPC(String, NetworkPlayer, Object[]) ConnectionScriptManager:Server_AssignSpawnPoint(NetworkPlayer) (at Assets/scripts/Network Scripts/ConnectionScriptManager.cs:270) UnityEngine.NetworkView:RPC(String, RPCMode, Object[]) ConnectionScriptManager:OnLevelWasLoaded(Int32) (at Assets/scripts/Network Scripts/ConnectionScriptManager.cs:252)
i try change playerNet with RPCMode.All and obviusly work, but for example if 2 players play, 4 character controller is spawned(2(number of players) per 2(total number of methods))
anyone can help me? :S thanks in advance :)
P.S. playerList have already list of player connected, the problem is only for call method from specific client(or server)
Answer by PixelFireXY · Dec 13, 2012 at 11:56 AM
Find! :D i don't know why dont work but if i change this piece:
[RPC]
public void Server_AssignSpawnPoint(NetworkPlayer playerNet)
{
if (Network.isServer)
{
for (int i = 0; i < playerList.Count; i++)
{
if (playerList[i].plNetwork == playerNet)
{
networkView.RPC("Client_SpawnPlayer", playerNet, i);
break;
}
}
}
}
[RPC]
public void Client_SpawnPlayer(int index)
{
GameObject playerNet = Network.Instantiate(player, spawnPoint[index].transform.position, spawnPoint[index].transform.rotation, 0) as GameObject;
}
with this:
[RPC] public void Server_AssignSpawnPoint(NetworkPlayer playerNet) { if (Network.isServer) { for (int i = 0; i < playerList.Count; i++) { if (playerList[i].plNetwork == playerNet) { print(playerNet); networkView.RPC("Client_SpawnPlayer", RPCMode.All, i, playerNet); break; } } } }
[RPC]
public void Client_SpawnPlayer(int index, NetworkPlayer plNet)
{
if (Network.player == plNet)
{
GameObject playerNet = Network.Instantiate(player, spawnPoint[index].transform.position, spawnPoint[index].transform.rotation, 0) as GameObject;
}
}
works perfectly, anyway if anyone know why i receve error with previous code please answer me. Thanks anyway at all :D
Your answer
Follow this Question
Related Questions
How do I create an Initial Spawn Point? 2 Answers
RPC Call Error/Failed 1 Answer
Network initializing player 1 Answer
Add spawn to script [Multiplayer] 0 Answers
move particle direction with player 1 Answer