Damage script

I posted a question a while ago about a damage script and thought I had fixed it but I have now got a new error I am pretty new to both unity and photon and I am baffled by this problem.

I created a damage script;
[code2=csharp]using UnityEngine;
using System.Collections;
public class CombatScript: Photon.MonoBehaviour {
public int hp = 1;

void ApplyDamage(int Damage)
{

hp -= (int)Damage;
if (hp <= 0)
{

}
else
{
photonView.RPC("setHP", PhotonTargets.Others, hp);
}
}
public void UpdateHP(int hitPoints)
{
hp = hitPoints;
}
void Update ()
{
DontDestroyOnLoad(this);
if(hp <= 0)
{
Debug.Log(photonView.viewID + " is DEAD! ");
}

}
[RPC]
void setHP(int newHP)
{
hp = newHP;
}
}[/code2]

The updatehp is called in a different script i use for character stats.

Now when i run both the editor version and the compiled version the editor is the only one that can deal damage, and also takes damage when attacking and is the only one that dies, while the compiled version gives this error;
PhotonView with ID 1001 has no method "setHP" marked with the &#91;RPC&#93;(C#) or @RPC(JS) Property! Args: int32

I think the problem is code i use to instantiate the character plus scripts as only the editor version seems to be the only one to the damage/combat script instantiated
Here is the code i use to instantiate the character
[code2=csharp]GameObject player = PhotonNetwork.Instantiate("PlayerLightSanguine", Vector3.zero, Quaternion.identity, 0);
player.AddComponent<ThirdPersonCamera>();
player.AddComponent<ThirdPersonController>();
player.AddComponent<DontDestroyScript>();
player.AddComponent<CombatScript>();
Camera.main.gameObject.AddComponent<SmoothFollow>().target = player.transform;
Camera.main.gameObject.AddComponent<SmoothLookAt>().target = player.transform;
Camera.main.gameObject.AddComponent<MouseOrbit>().target = player.transform;[/code2]

The DontDestroyScript is just so it doesn't destroy on loading a new scene.

Comments

  • Maybe PUN is not able to find the RPC method, cause you added the script after it got created.
    To check, please add the combat script to the prefab and skip the AddComponent for it. Check if that works and let us know.

    In worst case, we could enable you to check for suitable RPC methods on demand (after you added the components).
  • If I put the scripts on the model prefab it means they all deal damage when one is clicked, Rather than just one.

    Edit: Okay so i attempted a slight work around by adding a photonview.ismine statement to the fire function. Here is my script that interacts with combat script(Both of which are on the prefab.

    [code2=csharp]public class SanguineScript : Photon.MonoBehaviour {

    public int damage = 50;
    public int range = 5;
    public int hp = 200;

    void Start()
    {
    SendMessage("UpdateHP", hp,SendMessageOptions.DontRequireReceiver);
    }
    [RPC]
    void MeleeCombat()
    {
    Vector3 direction = transform.TransformDirection(Vector3.forward);
    RaycastHit hit;
    // Did we hit anything?
    if (Physics.Raycast(transform.position, (direction * 2) / 2, out hit))
    {
    Debug.DrawRay(transform.position,(direction * 2) / 2,Color.blue);
    hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

    }
    }
    void Update () {
    Vector3 direction = transform.TransformDirection(Vector3.forward);
    if (photonView != null)
    {
    if (photonView.isMine)
    {
    if (Input.GetButtonDown("Fire1"))
    {
    MeleeCombat();
    Debug.Log(photonView.viewID + " is Shooting");
    }
    }
    }
    }
    }[/code2]

    However this throws out a weird bug, Both players can damage each other just fine, However the player within an external build of game cannot be damaged once he hits 50 hp.
  • You need to call photonView.RPC("MeleeCombat", ...)