- Home /
Multiplayer - Only host updates, client won't call methods or update
Hi, I've been racking my brain for hours on this and can't seem to figure it. Basically I'm making a card based game (kind of like simplified Magic), and I have a CardManager and 2 Players. When the player plays the right cards, it sends an attack to the other player. I'm using the Card Manager to do all the logic, and then calling the attack on the Player script.
The problem is that it works fine when playing as the host, but the client doesn't seem to even call the Attack method. ANy help would be greatly appreciated.
Player Script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;
public class PlayerScript : NetworkBehaviour {
[SyncVar(hook="OnChangeHealth")]
public int health = 100;
public Image healthBar;
bool movedHealthPanel;
GameObject enemyPanel;
GameObject healthPanel;
public GameObject gameManager;
// Use this for initialization
public override void OnStartLocalPlayer ()
{
healthBar.transform.parent = GameObject.Find("LocalPanel").transform;
gameManager = GameObject.Find ("CardManager");
gameManager.GetComponent<CadManager>().player = this;
}
// Update is called once per frame
void Update () {
if (!movedHealthPanel) {
if (!isLocalPlayer) {
healthBar.transform.parent = GameObject.Find("EnemyPanel").transform;
healthBar.GetComponent<RectTransform> ().anchoredPosition3D = new Vector3 (0, 0, 0);
movedHealthPanel = true;
}
}
if (!isLocalPlayer) {
return;
}
}
[Command]
public void CmdTakeDamage(int amount){
// if (!isServer) {
// return;
// }
RpcDamage (amount);
Debug.Log("Damage Taken on Player: "+netId+" - "+health);
}
void OnChangeHealth(int health){
this.health = health;
healthBar.fillAmount = (float)health / 100;
}
[ClientRpc]
void RpcDamage(int amount){
health -= amount;
Debug.Log ("took " + amount + " damage");
}
public void Attack(int power){
GameObject[] players = GameObject.FindGameObjectsWithTag ("Player");
Debug.Log ("Players: "+players.Length);
foreach (GameObject p in players) {
if (p.GetComponent<PlayerScript>().netId != netId) {
Debug.Log ("Attacked Enemy: "+p.GetComponent<PlayerScript>().netId);
ClientScene.FindLocalObject(p.GetComponent<PlayerScript>().netId).GetComponent<PlayerScript>().CmdTakeDamage(power);
Debug.Log ("Enemy Health: "+p.GetComponent<PlayerScript>().health);
}
}
}
}
And on my Card Manager I run this code: if (activeCardItem.type == Item.TypeOfItem.attack) { player.Attack (activeCardItem.power); }
Both my Player Scripts and my Card Manager have Network Identites.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135141im_/https://answers.unity.com/themes/thub/images/avi.jpg)