- Home /
ViewID + Sending Messages
Hello,
Correct me if I'm wrong, but isn't a viewID the ID that the network gives to a gameObject to keep a reference of it?
I'm trying to get my Network.Instantiated projectile to send a damage message to who ever it hits, but I'm struggling to do that. So below is the script attached to my Projectile (arrow). What was happening is on CollisionEnter, if the hit is a player then hit.collider.SendMessageUpwards(damage), and then destroy the projectile... But no damage was never received.
So then I though I might need to use an RPC, so what I'm trying to do is use the vewID from the collider, and send a damage message:
function OnCollisionEnter(hit : Collision){
if(hit.collider.gameObject.tag == "Player"){
var viewID = hit.collider.Network.viewID;
networkView.RPC("SendDamage", RPCMode.All, viewID, damage);
}
Network.Destroy(GetComponent(NetworkView).viewID);
}
@RPC
function SendDamage(viewID : NetworkViewID, damage : float){
var enemyID = NetworkView.Find(viewID);
var enemy = enemyID.observed.gameObject;
enemy.gameObject.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
But whats happening is that I get a NullException, at: var viewID = hit.collider.Network.viewID;
What do I need to do, to send damage to my collider object?
Answer by whydoidoit · May 24, 2012 at 11:21 PM
You need to be using:
hit.collider.networkView.viewID
Yes, like you guessed a ViewID connects the gameobject on different computers. The ViewID belongs to a NetworkView component. If you send the RPC with the networkview of your player, the gameobject that represents your player on the other computer will receive it.
Since you don't want to hurt yourself, you should send the RPC on the networkview of the object you want to hit.
And to do that I change the RPC$$anonymous$$ode to Others, correct?
Use Others only if you are processing the collision differently locally - use All if you want to receive the RPC on the object doing the sending too. This is normally easier.
I always like the "keep your code DRY" concept. (Dont Repeat Yourself). All allows you just to process the damage in an RPC call and not worry about the fact that you need to do something different on the sender.