- Home /
Not understanding [RPC]
(Edit for bump)
Alright, so I wrote a script for playerHealth and I'm trying to update it so each player over the network will have their own health, but I'm running into the problem where on Client A, I'll have 100 health, and my enemy will have 70 health, but on Client B, we will both have 100 health.
Just not understanding the RPC I guess, I tried using (networkView.isMine) on it, but that didn't work as the networkView is owned by the server. I just don't really know what I'm doing here, how would I make it update.
Lets just say for instance this scripts name is HealthManager and the variable is myHealth, just for an example.
==============================
I was asked for some code, so here's what I have, although, there isn't any RPCs because I'm trying to understand how to use them.
using UnityEngine;
using System.Collections;
public class PlayerStatistics : MonoBehaviour {
bool enabled = false;
//~~Variables Start
public int currHealth, maxHealth;
//~~Variables End
void Awake() {
enabled = true;
}
// Use this for initialization
void Start () {
if(enabled) {
maxHealth = 100;
currHealth = maxHealth;
}
}
// Update is called once per frame
void Update () {
if(enabled) {
}
}
void OnGUI() {
if(enabled) {
GUI.Box (new Rect(20, 20, Screen.width / 3, 30), "Health: " + currHealth + "/" + maxHealth);
}
}
}
here's the code I use to do combat
using UnityEngine;
using System.Collections;
public class PlayerCombat : MonoBehaviour {
bool enabled = true;
//~~Variables Start
public GameObject enemyTarget;
PlayerStatistics enemyStats;
//~~Variables End
void Awake() {
enabled = true;
}
// Use this for initialization
void Start () {
if(enabled) {
}
}
// Update is called once per frame
void Update () {
if(enabled) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Input.GetButton("Fire2")) {
if(Physics.Raycast (ray, out hit, 100)) {
if(hit.collider.tag == "Player") {
if(hit.collider.gameObject != gameObject) {
Debug.Log ("Clicked other player");
enemyTarget = hit.collider.gameObject;
} else {
Debug.Log ("Clicked self, not targetting");
}
}
}
}
if(enemyTarget != null) {
enemyStats = enemyTarget.GetComponent<PlayerStatistics>();
if(Input.GetKey(KeyCode.F)) {
enemyStats.currHealth -= 10;
}
}
}
}
void OnGUI() {
if(enabled) {
if(enemyTarget != null) {
GUI.Box (new Rect((Screen.width / 3) + (Screen.width / 3), 20, Screen.width / 3, 30), "Enemy Health: " + enemyStats.currHealth + "/" + enemyStats.maxHealth);
}
}
}
}
I posted it, although there are not any RPCs in it, because that's what I'm questioning / trying to understand
Stop bumping. It clogs up the comment section and helps no one!
You describe a specific problem related to RPC's, then you say you are not using RPC's and post code without RPC's. If you want to understand RPC's, look it up. There are plenty of very good explanations out there.
I also suggest you do some studying on networking to get a hold of the jargon :)