- Home /
Question by
j_perker · Feb 24, 2014 at 09:23 PM ·
guinetworkingnullreferenceexception
Trying to get an RPC to display the health of a player over their head
void OnGUI()
{
if(photonView.isMine)
{
GUI.Label(new Rect(500, 300, 100, 40), "Your hits left: " + playerHealth);
}
this.gameObject.GetComponent<PhotonView>().RPC ("ShowHP", PhotonTargets.All, playerHealth);
}
[RPC]
void ShowHP(int player_health)
{
Vector3 characterPos = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
characterPos.y = Screen.height - characterPos.y;
GUI.Label(new Rect(characterPos.x - 5, characterPos.y - 30, 30, 30), player_health.ToString());
}
This code should be displaying a gui label above a player's head indicating their HP and everyone should be able to see it. I'm getting a null reference exeception error on the line Vector3 characterPos = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
and I'm not really sure why. Any ideas?
Comment
The code works for the offline mode, so I'm not really sure why it's not working here.
Try Debug.Log() for the line with error components: characterPos, this.gameObject, Camera.main - to see the missing object; probably you have deactivated some objects by the server/client rules.