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.
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 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;
}
}