My health is reduced when I shoot

Options
Hello :

'm still noob with photon , I'm trying to shoot tanks and reduce their health , first experience I was reducing my own health when shooting , now I'm able to return target id but I'm not able to reduce enemy health:

I think that I have a problem with my RUNRpc method

I really appreciate your help

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class towerSettings : Photon.MonoBehaviour {
public GameObject explosion1;
public RaycastHit hit ;
Ray ray;
public AudioSource sound;
public GameObject camera;
public AudioClip snd;
public GameObject crosshair;
public Sprite redcrosshair,whitecrosshair;
public Slider healthBarSlider;
void start(){

}
void Update() {
ray = Camera.main.ViewportPointToRay (crosshair.transform.position);

Vector3 fwd = transform.TransformDirection (Vector3.up);

if (Physics.Raycast (transform.position, fwd, out hit, 500)) {

if (hit.transform.gameObject.tag == "Player") {
crosshair.GetComponent ().sprite = redcrosshair;

if (Input.GetButtonDown("Fire1")){
sound = GetComponent();
snd = sound.clip;
//StartCoroutine (destroytank ());
PhotonView photonView = hit.transform.gameObject.GetComponent();
int targetId = photonView.owner.ID;
PhotonPlayer target = PhotonPlayer.Find (targetId);
print("enemy is "+targetId); // works fine
Debug.Log("target is "+target); // works fine
//PhotonView photonView = this.photonView;
//photonView.RPC("ReduceHealth", PhotonTargets.AllBufferedViaServer); // Call to RPC funct
photonView.RPC("ReduceHealth", target); // Call to RPC funct
}
} else {
crosshair.GetComponent ().sprite = whitecrosshair;

}
} else {
crosshair.GetComponent ().sprite = whitecrosshair;


}}
public IEnumerator destroytank(){


AudioSource.PlayClipAtPoint(snd,camera.transform.position,1f);
yield return new WaitForSeconds (0.01f);
Destroy(hit.transform.gameObject);
}
[PunRPC]
public void ReduceHealth(PhotonPlayer target)
{

Reduce(10);
}
public void Reduce(float p){
healthBarSlider.value -= p;
Debug.Log (p + " " + healthBarSlider.value);
}

}

Comments

  • Loumi
    Options
    finally I disover it :

    I take damage on my tank prefab , but this script is attached to the tower prefab :

    hit.transform.GetChild(1).GetComponent().RPC("ReduceHealth", PhotonTargets.AllBufferedViaServer);