- Home /
RPC on network instantiated GameObject beats instantiation
I have a problem with network instantiated GameObjects in that RPC's on the instantiated GameObject gets called before the actual instantiation on the clients.
Let me demonstrate, consider this code allocating the viewId and creating the GameObject over the network:
private void NetCreateClient(string name, bool bot)
{
var viewId = Network.AllocateViewID();
networkView.RPC("rpc_new_Client", RPCMode.AllBuffered, viewId, name, bot);
}
[RPC]
private void rpc_new_Client(NetworkViewID viewId, string name, bool bot)
{
var client = Instantiate(ClientPrefab) as Client;
client.networkView.viewID = viewId;
client.NetInstantiated(name, bot);
if (Network.isServer && _gameModeType == 2)
{
client.SetTeam(IsAllied ? Teams.Allied : Teams.Soviet);
IsAllied = !IsAllied;
}
}
Now, on the server, this code is executed on the instantiated GameObject above:
public void SetTeam(Teams team)
{
networkView.RPC("rpc_SetTeam", RPCMode.AllBuffered, (int)team);
}
[RPC]
private void rpc_SetTeam(int teamId)
{
Team = (Teams)teamId;
}
The problem is that on the client (not the server), the RPC for the instantiated GameObject, "rpc_SetTeam", is executed BEFORE the actual instantiation takes place on the client. This leads to the dreaded "Couldn't invoke rpc function because the networkview doesn't exist" error.
This is strange in my opinion, as I was under the impression that an RPC cannot take place on something that does not exist. Clearly this is not the case. Can someone explain how this is meant to work? I guess we can never be sure that the GameObject has been created on a specific client, so when is it safe to start sending RPC's then?
Your answer
![](https://koobas.hobune.stream/wayback/20220613173732im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Buffered RPCs Only Sent To One New Client 0 Answers
Network Instantiate PlayerPrefab Problem 2 Answers
problem with RPC instantiate on network 2 Answers
Client does not recieve host's Network.Instantiate during game setup 0 Answers
RPC sync issues 0 Answers