- Home /
Multiplayer do damage
So right now I am trying to make it so if i click on the other person, it does damage to that character. I started the game on Unity and also built and ran it to so I can have two instances of the game running. What happens is that each instance connects to the photon server and spawns the character and right now I can move each character individually.
The thing is on the unity instance, it spawns both characters both with 100 health, and when I click on the other person, it debugs their health and it works. The other player's health goes down by 10, and if i click on myself, my health goes down by 10. But on the other instance of the game, both characters are still at 100 health, and when i do the same thing, both health goes down with i click on them. So the problem is they are not updating on each other's instance even though when i move each character, i can see them updating on the other instance of the game. Here is my code for the damage.
using UnityEngine;
using System.Collections;
public class Stats : Photon.MonoBehaviour {
public float healthBarLength;
public Texture2D bgImage;
public Texture2D fgImage;
public int maxHealth = 100;
public int health;
public int attack = 100;
RaycastHit hit;
Ray ray;
void Start()
{
health = maxHealth;
healthBarLength = Screen.width /2;
}
void Update()
{
AddjustCurrentHealth(0);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (photonView.isMine)
{
if (Input.GetMouseButtonUp(2))
{
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "Player")
{
hit.collider.gameObject.GetComponent<Stats>().AddjustCurrentHealth(-10);
Debug.Log(hit.collider.gameObject.GetComponent<Stats>().health);
}
}
}
}
}
void OnGUI () {
if (photonView.isMine)
{
GUI.BeginGroup (new Rect (0,0, healthBarLength,32));
GUI.Box (new Rect (0,0, healthBarLength,32), bgImage);
GUI.BeginGroup (new Rect (0,0, health / maxHealth * healthBarLength, 32));
GUI.Box (new Rect (0,0,healthBarLength,32), fgImage);
GUI.EndGroup ();
GUI.EndGroup ();
}
}
public void AddjustCurrentHealth(int adj){
health += adj;
if(health <0)
health = 0;
if(health > maxHealth)
health = maxHealth;
if(maxHealth <1)
maxHealth = 1;
healthBarLength =(Screen.width /2) * (health / (float)maxHealth);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613201303im_/https://answers.unity.com/themes/thub/images/avi.jpg)