I found the problem. The code itself was working great (and you're right pek, that bar parameter was just tedious).
The problem was actually with the ex2D plugin I was using for the health bar. On each exSprite there is a camera view, set to the user's main camera. Because it was set to my camera on player instantiate, it was only seeing my bar through my camera, thus not updating the other bar via client / server side. By clicking on the texture and setting the ex2D exSprite's Camera to None, I can now see both bars being updated / scaled properly.
In hopes that this can help anyone looking for how to do health bars over a network, here is the final code I'm using:
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour
{
public int health;
public int maxHealth;
public float healthBarLength = 0;
public exSprite healthBar;
void Start()
{
if(!networkView.isMine)
{
enabled = false;
}
}
void Update ()
{
if(Input.GetKey(KeyCode.Alpha2))
{
Minus_Health();
Update_HP(health, maxHealth);
}
}
public void Minus_Health()
{
health -= 10;
}
public void Update_HP(int hp, int maxHP)
{
networkView.RPC("Update_Health", RPCMode.AllBuffered, hp, maxHP);
}
[RPC]
public void Update_Health(int value, int value2)
{
health = value;
maxHealth = value2;
healthBarLength = (float)value / (float)value2 / 5.1f;
healthBar.scale = new Vector2(healthBarLength, healthBar.scale.y);
}
}
Also, a small tip for those who get errors when setting the exSprite Camera to None; You need to update your ex2D plugin.
Thanks for the help pek and I hope this can help someone! :)