- Home /
My Photon health script doesnt work
I am using photon for my game and I have one player gameobject but i does not work
here my script: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Combat : Photon.MonoBehaviour { public float Health = 100; private float MinHealth = 0; private float MaxHealth = 100;
public Slider slider;
public virtual void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(Health);
} else if (stream.isReading)
{
Health = (float)stream.ReceiveNext();
}
}
private void Update()
{
slider.value = Health;
}
[PunRPC]
void Damage()
{
Health -= 10;
}
} and in another script when i call the function: photonView.RPC("Damage", PhotonTargets.All, coll.gameObject); coll means collider it's the overlapping player.
Answer by Captain_Pineapple · Apr 28, 2019 at 01:31 PM
Hey there and welcome to the forum,
your problem is most likely that when you call photonView.RPC("Damage", PhotonTargets.All, coll.gameObject);
you pass an argument to Damage
which you did not define in your function. Leave the coll.gameObject out there or add an gameobject parameter to your Damage
function.
Secondly always avoid sending redundant data. This means that you should only update the health value when it actually changes. (Like you do by using the damage rpc - this is good) By adding the health value to your stream you only add 4 byte of unnecessary data that is sent 10 times per second to each player in your room.
If something was not clear or you need more help on this let me know and i'll try to explain it a bit more detailed.
ok at first thank you for your answer but how can I then send it specific to the collided player
I look into this forum once or twice a day, begging for an answer won't speed this up ;)
Answer by TheGeorg · Apr 28, 2019 at 06:28 PM
Ok it still dont works I send you both of my scripts:
Combat script: using UnityEngine; using System.Collections; using UnityEngine.UI; using Photon;
public class Combat : Photon.PunBehaviour { public float Health = 100; private float MinHealth = 0; private float MaxHealth = 100;
public Slider slider;
public virtual void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(Health);
} else if (stream.isReading)
{
Health = (float)stream.ReceiveNext();
}
}
[PunRPC]
void Damage(GameObject WhichPlayer)
{
Health -= 10;
slider.value = Health;
}
}
WhichCollider script: using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon;
public class WhichCollider : PunBehaviour { public GameObject player; public Collider2D coll;
private bool CanHit;
private bool CanHit2;
private void OnTriggerEnter2D(Collider2D collision)
{
coll = collision;
CanHit = true;
}
private void OnTriggerExit2D(Collider2D collision)
{
CanHit = false;
}
private void Update()
{
if (CanHit == true)
{
if (player.GetComponent<AnimController>().Flipped == true)
{
if (gameObject.name == "Collider1")
{
if (player.GetComponent<AnimController>().WaitForHit == true)
{
if (coll.gameObject.CompareTag("Player"))
{
if (CanHit2 == false)
{
CanHit2 = true;
print("Links");
StartCoroutine(SetCanHit());
}
}
}
}
}
if (player.GetComponent<AnimController>().Flipped == false)
{
if (gameObject.name == "Collider2")
{
if (player.GetComponent<AnimController>().WaitForHit == true)
{
if (coll.gameObject.CompareTag("Player"))
{
if (CanHit2 == false)
{
CanHit2 = true;
print("Rechts");
StartCoroutine(SetCanHit());
}
}
}
}
}
}
}
IEnumerator SetCanHit()
{
yield return new WaitForSeconds(0.3f);
photonView.RPC("Damage", PhotonTargets.Others);
CanHit2 = false;
}
}
Ok well first of all remove the photon stream. you probably wont need this.
I adjusted your script for you. Please try to understand the code and do not blindly copypaste it.
public class Combat : Photon.PunBehaviour {
public float Health = 100;
private float $$anonymous$$inHealth = 0;
private float $$anonymous$$axHealth = 100;
public Slider slider;
public virtual void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info)
{
// only add variables here that basically change every frame, like positions and rotations ( mostly already handled by networktransform)
if (stream.isWriting)
{
} else if (stream.isReading)
{
}
}
/// <summary>
/// use this function to access this script and deal damage to this player or object
/// make sure that this script is on your main player character.
/// </summary>
public void damagePlayer(float value)
{
Health -= value;
slider.value = Health;
photonView.RPC("syncHealth", PhotonTargets.Others, value);
}
/// <summary>
/// this is used when you have called the damagePlayerfunction. This should sync the values over all instances.
/// </summary>
[PunRPC]
void syncDamage(float damage)
{
Health -= damage;
slider.value = Health;
}
}
This way when you want to damage an object or player that has a photon view and the combat script you can simply call the damagePlayer
function and it should do the rest. it will apply the new value locally and call the rpc to set the new health value for everyone else.
Falls dir das in englisch zu schwierig ist sag bescheid, dann bekommste das nochmal übersetzt.
Ok at first thank you verrrryyy much now it works but only on the device which applies the damage so the health variable is on the other device still 100. I got this error: PhotonView with ID 2001 has no method "syncHealth" marked with the [PunRPC](C#) or @PunRPC(JS) property! Args: Single UnityEngine.Debug:LogError(Object)
but its marked with the PunRPC
Your answer
Follow this Question
Related Questions
Photon Combat script does not work 0 Answers
PhotonUnityNetworking UnityException 0 Answers
PUN - Choose random Hero for player 0 Answers
Decrase value on collision C# 2 Answers
My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers