'Unity Photon PUN RPC call
I have a question about RPC and Photon PUN.
I have 1 GameObject in my game that calls an RPC method. This game object contains a C# script (that contains the RPC method) and the PhotonView component with/without (is should be the same) the observed script and the ObserveOption equals to Off.
I have a character which have to shoot to a cube. This cube has the previously described script and component attach to it.
The RPC is sent once (when the cube has been collided with a projectile) but in my scene I have many Cube which should receive the message because I have to decrease the health of the hit cube. The problem is that only 1 Cube receive the RPC call (The RPC is caught by the first instantiated object at the start of the game) but the RPC should be received by all the cube (is that right?) in my scene.
The cubes are exactly the same but they have a different photonView ID (rightly) so we could check whether the hit cube to which decrease the health is the right one.
This is the significant part of the code:
public static void reduceHealthRPC(float damage, int viewID)
{
photonView.RPC("reduceHealth", PhotonTargets.All, damage, viewID);
}
[RPC]
public void reduceHealth(float damage, int viewID)
{
if(this._viewID != viewID)
{
Debug.Log ("The view ID is not mine. My viewID is: " + _viewID + ", and the one which is coming form RPC is: " + viewID);
return;
}
currentHealth -= damage;
Debug.Log("My life is: " + currentHealth);
}
Please help me to unerstand what I'm doing wrong. Thank you.
Solution 1:[1]
If you need call RPC for all cubes, do it for every cube. Currently you are calling RPC only for object referenced by static 'photonView' variable. Depending on how it's initialized, RPC called on that object.
Solution 2:[2]
I might be late ,but try PhotonTargets.AllBuffered
,that will make the RPC call run on players that join later .
Solution 3:[3]
Might be super late but have you tried this? cube.cs
is attached to all cubes.
in main.cs
cube[] allCubes = FindObjectsOfType<cube>();
for (int x = 0; x < allCubes.length; x++)
allCubes[x].reduceHealthRPC(dam,id);
this will send the code to all cubes with one target id, if this is your goal but you are much better to just rpc on the individual cube.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | photonians |
Solution 2 | Yaser Alosh |
Solution 3 | roottraveller |