- Home /
Sending a gameobject over network with RPC?
Hello,
I am having the exact same problem as this post: http://answers.unity3d.com/questions/11113/buffered-rpcs-from-networkinstantiate-not-removed.html I've also read the RPC details: http://unity3d.com/support/documentation/Components/net-RPCDetails.html
So there I read the answer AngryOldMan, which sounds like a good solution. The problem is, I can't send GameObjects using RPC calls ("not supported" in unity the error says). How do I solve this then if I cant send the objects?
For example, here's what my clients execute:
public void ClientKillFrog(GameObject frog)
{
// send a request to the server to kill a gameobject
networkView.RPC("OnClientKillFrog",RPCMode.Server, frog);
}
[RPC]
void OnServerKillFrog(GameObject frog)
{
// server has ack'd a clients need to kill a gameobject
Destroy(frog);
}
And here's the server:
[RPC]
void OnClientKillFrog(GameObject frog,NetworkMessageInfo info)
{
Debug.Log("OnClientKillFrog");
networkView.RPC("OnServerKillFrog",RPCMode.AllBuffered,frog);
}
UPDATE: OK, solution by jet.c works in some cases, but I have one more problem: When I have a server running and 1 client playing, killing and spawning some frogs, then when another client connects I get this:
View ID AllocatedID: 1 not found during lookup. Strange behaviour may occur
Couldn't perform remote Network.Destroy because the network view 'AllocatedID: 1' could not be located.
....
I assume this is because the newly connected client tries to perform Network.Destroy on objects already destroyed for everyone else, so the server crashes. How do you handle this?
Do you use Network.Instantiate for all objects that have to interact over the network? You have to understand how networking works. Every peer has it's own objects. They have nothing in common except the NetworkViewID. This ID has to be unique across the whole network and link the objects on different PC into one object.
If you create an object you have to create it on all clients as well. Network.Instantiate does this for you. It also stores a buffered RPC that tells new connected players which objects has been created in the past. When you destroy an object with Network.Destroy (which destroys the local object and tells all clients to destroy their object with the same NetworkViewID) you also have to remove the buffered RPCs with Network.RemoveRPCs to prevent new clients from creating / destroying objects that doesn't exist anymore.
btw. The question you refer to is outdated. Back these days there wasn't a Network.RemoveRPCs that takes a NetworkViewID as parameter.
This function has been introduced in Unity 3.2 as you can read here: UpdateNotes Unity 3.2(see "Other Improvements"). So you also have to be careful with tutorials before that release since there has been invented a lot complicated workarounds.
Answer by jef.c · Jan 16, 2012 at 09:09 AM
Rather than sending a GameObject through the RPC, try sending its NetworkViewID instead.
but how do you call Destroy on it then? since you cant just Destroy on a networkViewID. Ive tried Network.Destroy() but it keeps throwing me exceptions such as "View ID AllocatedID: 2 not found during lookup. Strange behaviour may occur" during runtime
Try clearing the RPCs on it right before you destroy it: Network.RemoveRPCs( networkViewID );
Answer by elliselkins · Nov 01, 2017 at 09:17 PM
Turns out you can have a GameObject parameter in an Rpc or Command function (see the Arguments to Remote Actions section). The GameObject must have a NetworkIdentity component, so it must either be in the scene when the application runs or after it is instantiated then it must be spawned with NetworkServer.Spawn.