Damage dealing scripts

Options
Okay i posted this question a whiles back and didn't really get the help i needed, i'm trying to set up third person combat but i can't seem to deal damage to the other player i even purchased the M2H guides and tried using the fps example i'll show how far I've got then i'll explain more
[code2=csharp]public int damage = 50;
public int range = 10;

void MeleeCombat()
{
Vector3 direction = transform.TransformDirection(Vector3.forward);
RaycastHit hit;


// Did we hit anything?
if (Physics.Raycast(transform.position, direction * 2, out hit))
{
hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}


}

// Update is called once per frame
void Update () {
Vector3 direction = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position,direction * 2,Color.blue);
if (Input.GetButtonDown("Fire1"))
{
MeleeCombat();
}
}

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{

}
else
{

}
}[/code2]
This is my damage dealing script and is attached to an empty gameobject on the player prefab
[code2=csharp]public int hp = 100;


void ApplyDamage(int Damage)
{

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

}
else
{
photonView.RPC("setHP", PhotonTargets.Others, hp);
}
}
void update ()
{
if(hp <=0)
{
//Destroy(this.gameObject);
}
}

[RPC]
void setHP(int newHP)
{
hp = newHP;
}[/code2]
This is the part that receives the damage and is on the main player model in the player prefab.

However this script doesn't work and i can't figure out why.

A few things to note;
When i run two instances of the game the combat script doesn't show up in the inspector on whichever model is on the instance of the game run from the built version.

Also using these scripts i get two errors that i've searched high and low for a solution and got nothing here's the errors;
ERROR You have missing MonoBehaviours on your gameobjects!
UnityEngine.Debug:LogError(Object)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1729)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1490)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:76)
PhotonView with ID 1001 has 2 methods "setHP" that takes 1 argument(s): Int32. Should be just one?
UnityEngine.Debug:LogError(Object)
PhotonHandler:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:170)
NetworkingPeer:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:799)
NetworkingPeer:ExecuteRPC(Hashtable, PhotonPlayer) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1852)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1490)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:76)

If anyone could guide me in the right direction that would be nice.

Comments

  • Tobias
    Options
    Maybe the HP setting doesn't work, because you have those errors.
    Let's fix them first.

    "You have missing MonoBehaviours on your gameobjects!"
    I had to look this up but I can't yet explain it. With photonNetview.GetComponents<MonoBehaviour>(), we get all MonoBehaviours on the gameObject that we target. For some reason, this is null, causing the output.
    Usually, this doesn't happen. Maybe you remove a component from your GameObjects or do something similar with it.
    Find out which GameObject this is and check if you have scripts on that. You can't call RPCs on GameObjects which don't have scripts (which implement the RPC).

    PhotonView with ID 1001 has 2 methods "setHP" that takes 1 argument(s): Int32. Should be just one?
    This error sounds like you didn't copy all of the relevant code into your post. You seem to have 2 implementations of setHP(someParameter).
    Try to find the duplicate and get rid of it. You can use multiple names for RPCs to do different things.
  • Just writing to say i fixed the errors and now it works fine, thanks for the help;

    The first error i had another script that i forgot to disable and it was activating on "fire1" and that was were the mono behavior error was coming from.
    The second error had to to with the same script, i just lost it deep in my files and didn't realize it was still attached to the game object. Thanks for the help though helped a lot.