- Home /
RPC Call to Another GameObject's Function?
I'm attempting to use an RPC call to send damage information to the other player in a 2 player Network game. However, I'm a bit confused on how to get the "Receiver" to acknowledge the change:
@RPC
function GiveDamage(hitDamage : float)
{
Network.Instantiate(hitPrefab, Vector3(selectedEnemy.transform.position.x, 1.7, selectedEnemy.transform.position.z), Quaternion.identity, 0);
networkView.RPC ("selectedEnemy.GetComponent(playerObject).TakeDamage", RPCMode.All, hitDamage);
//selectedEnemy.GetComponent(playerObject).TakeDamage(hitDamage);
}
The commented out line works on the local machine (and the damage is taken, etc), but on the other players machine, nothing changes. The networkView.RPC
line is taken from the RPC instructions, but I think I'm confused how to specify the object that is to receive the instructions to run the function. (http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html)
Any ideas?
This works, but it's not clean so I need to make it a more simple setup: (Note: This script is on both player & enemy objects)
@RPC
function GiveDamage(hitDamage : float)
{
selectedEnemy.GetComponent(playerObject).TakeDamage(hitDamage);
}
@RPC
function RemoveHitPoints(damage : float)
{
hitPoints = (hitPoints - damage);
if (hitPoints <= 0)
{
DidDie();
}
}
@RPC
function TakeDamage(damage : float)
{
networkView.RPC ("RemoveHitPoints", RPC$$anonymous$$ode.All, damage);
}
Basically in the game when a player object "Attacks" the enemy object (player object of another player), GiveDamage() runs, telling enemy object to TakeDamage(). TakeDamage() then broadcasts the RemoveHitPoints() function to all clients. Basically it seems that networkView.RPC commands should only be run FRO$$anonymous$$ the same script they're going to access. If I'm wrong, please someone let me know :)
Answer by jjmontes · Apr 19, 2014 at 12:33 PM
You can receive RPC calls to methods in any script which is a component of the GameObject that contains the NetworkView component.
Thus, you can spread your RPC methods over different scripts, as long as all of them have been attached as components to the same GameObject containing a NetworkView.
Your answer
Follow this Question
Related Questions
View ID AllocatedID: # not found during lookup. Strange behaviour may occur 1 Answer
Acess server stuff by the client 0 Answers
How to use Unet to send network messages like RPC (Javascript) 0 Answers
how to properly clean up the NetworkView to prevent errors ? 0 Answers
RPCMode.AllBuffered Basic Question 1 Answer