Problem Calling RPCs
Options
Hello,
I'm developing a small cooperative multiplayer game, where the players have to kill all the enemies hordes. I still don't know if I'm doing everything right because my only experience with PUN is a FPS multiplayer with only one object (the player).
I created a general script called HealthSystem that recieves the damage from something. It's general because I use this for both the player and the enemies, each one with their own lives. I created a public function called LoseHealth that recieves the amout of damage from the outside: Enemies from the player's bullet and the Player from the robot missle and the minion attack. I know that LoseHealth have to be a PunRPC, but I don't know how to call it from the others scripts, I'm just recieving a NullReference error.
Here is my codes:
HealthSystem
public void CallLoseHealth(float _damage){
this.photonView.RPC("LoseHealth", RpcTarget.All, _damage);
}
[PunRPC]
public void LoseHealth(float damage) {
Debug.Log(gameObject.name = " lost health");
healthCounter -= damage;
}
Minion Attack
void Update()
{
float distance = Vector3.Distance(transform.position, player.position);
if(distance <= lookRadius){
agent.SetDestination(player.position);
if(distance <= agent.stoppingDistance){
this.photonView.RPC("FaceTarget", RpcTarget.All);
this.photonView.RPC("Attack", RpcTarget.All);
}
}
}
[PunRPC]
void Attack(){
explosion.Explode();
player.GetComponent<HealthSystem>().CallLoseHealth(attackDamage);
}
Projectile (also general)
void OnCollisionEnter(Collision other) {
if(other.gameObject.GetComponent() != null) {
other.gameObject.GetComponent().CallLoseHealth(damage);
}
Destroy(gameObject);
}
Both the Player and the Enemies have the PhotonView component, and any others updates (transform, shooting function) are working fine. What's happening?
Thank you!
I'm developing a small cooperative multiplayer game, where the players have to kill all the enemies hordes. I still don't know if I'm doing everything right because my only experience with PUN is a FPS multiplayer with only one object (the player).
I created a general script called HealthSystem that recieves the damage from something. It's general because I use this for both the player and the enemies, each one with their own lives. I created a public function called LoseHealth that recieves the amout of damage from the outside: Enemies from the player's bullet and the Player from the robot missle and the minion attack. I know that LoseHealth have to be a PunRPC, but I don't know how to call it from the others scripts, I'm just recieving a NullReference error.
Here is my codes:
HealthSystem
public void CallLoseHealth(float _damage){
this.photonView.RPC("LoseHealth", RpcTarget.All, _damage);
}
[PunRPC]
public void LoseHealth(float damage) {
Debug.Log(gameObject.name = " lost health");
healthCounter -= damage;
}
Minion Attack
void Update()
{
float distance = Vector3.Distance(transform.position, player.position);
if(distance <= lookRadius){
agent.SetDestination(player.position);
if(distance <= agent.stoppingDistance){
this.photonView.RPC("FaceTarget", RpcTarget.All);
this.photonView.RPC("Attack", RpcTarget.All);
}
}
}
[PunRPC]
void Attack(){
explosion.Explode();
player.GetComponent<HealthSystem>().CallLoseHealth(attackDamage);
}
Projectile (also general)
void OnCollisionEnter(Collision other) {
if(other.gameObject.GetComponent() != null) {
other.gameObject.GetComponent().CallLoseHealth(damage);
}
Destroy(gameObject);
}
Both the Player and the Enemies have the PhotonView component, and any others updates (transform, shooting function) are working fine. What's happening?
Thank you!
0