- Home /
Network RPC find sender gameobject
Hi.
How can I find the Gameobject who "sent" the RPC?
Thanks in advance.
Andreas.
Answer by ItsAaronLol · Aug 09, 2014 at 06:53 AM
I ran into this problem too, but i've finally solved it.
Say you have a bunch of objects on the map owned by different players and you want to destroy a player's object when the player presses the 'F' key...
Provided each of the objects on the map has a Network View attached to it, each object should have its own NetworkViewID.
In this scenario, each object has a tag called "Player".
You need to call an RPC method that takes in the NetworkViewID of the player who pressed 'F' key. This NetworkViewID is then checked against all the other player's object's NetworkViewIDs to see if they match.
void Update () {
if(Input.GetKey("f")){
GameObject[] players;
players = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject player in players){
if(player.networkView.isMine){
networkView.RPC ("destroyMe", RPCMode.AllBuffered, player.networkView.viewID);
}
}
}
}
[RPC] public void destroyMe(NetworkViewID viewID){
GameObject[] players;
players = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject player in players){
if(player.networkView.viewID == viewID){
Destroy (player, 0);
}
}
}
I hope this makes sense.
Answer by getyour411 · Jan 12, 2014 at 07:49 PM
To do this, you need to define the NetworkMessageInfo parameter in your first RPC method like here: http://unity3d.com/support/documentation/Components/net-RPCDetails.html
Then inside this RPC method you get the sender object from the NetworkMessageInfo and use it to call a RPC-method on the original sender. For this you use the second type of RPC call described here (the one that uses NetworkPlayer as a target): http://unity3d.com/support/documentation/ScriptReference/NetworkView.RPC.html